var readline = require('readline');
var reader = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
function stdinput(){
reader.on('line', function (cmd) {
return cmd;
});
}
console.log(stdinput());
output:
undefined
stdinput() function giving "undefined" before reading input from stdin. I searched many resources but not able understand why it's interpreting Asynchronously.
I am writing CLI interactive application in nodejs. it reads input many times. If we use recursion to read, it takes more stack memory and If we use callbacks, Promise and Async/await here also getting undefined before stdinput() i.e below part of the code is executing first before reading input.