How to change variable inside readline function?

I have following code and I don't know how to change variable x inside the function.

var readline = require('readline');
var r1 = readline.createInterface({
   input: process.stdin,
   terminal: false 
});
var x = 1;
r1.on('line', function(inputText) {
    x = 2; // i want to be able to change it here
}
console.log(x); //prints 1 and I want 2

Answers


The variable will change - it's just that your function is asynchronous - so your log outside the on('line') function is executing before the variable assignment.


Need Your Help

How can i know the swinging direction of the ios device in ios programming?

iphone gravity uiaccelerometer cmmotionmanager

I'm new to IOS programming, I don't know how to judge the swinging direction of a device.And I have searched a long time for this question, but I only find very little about this. Could someone hel...