I am creating a module to get experience and shorten some code. I have a piece of code which uses readline in a simplified manner, like var x = arkin.question("How old are you? ");
. Readline doesn't wait for the answer. It produces this:
How old are you? undefined
Code:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
exports.question = function(q){
var response;
rl.setPrompt(q);
rl.prompt();
rl.on('line', (userInput) => {
response = userInput;
rl.close();
});
rl.on('close', () => {
return response;
});
}
I call it like this:
var age = arkin.question("How old are you? ");
console.log(age);
I have tried using this code:
rl.question(q, (userInput) => {
rl.close;
response = userInput;
return response;
});
Yet I get the same result. Thanks in advance for your help.