I put together a script for grabbing user input via readline. The simplified version of the logic that's failing is as follows:
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function ask() {
return new Promise((resolve, reject) => {
rl.question('Enter input:', (input) => resolve(input) );
});
}
async function logic() {
let result = await ask();
console.log(result); // this console.log seems fine
rl.close()
return result;
}
let a = logic();
console.log(a); // this console.log causes the issue
When I run the above, the output is:
Enter input: Promise { <pending> }
The culprit seems to be console.log()
following the call to logic()
. If I remove it, data is requested as expected and operation continues. But getting rid of all log messages in the code after readline won't work for this script. What can I do to fix this? Do I need to do something to process.stdout after closing input?