How to readline infinitely in Node.js
Another option, via the Node.js documentation, is to use events:
var readline = require('readline'),
rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('OHAI> ');
rl.prompt();
rl.on('line', function(line) {
switch(line.trim()) {
case 'hello':
console.log('world!');
break;
default:
console.log('Say what? I might have heard `' + line.trim() + '`');
break;
}
rl.prompt();
}).on('close', function() {
console.log('Have a great day!');
process.exit(0);
});
var readline = require('readline');
var log = console.log;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var recursiveAsyncReadLine = function () {
rl.question('Command: ', function (answer) {
if (answer == 'exit') //we need some base case, for recursion
return rl.close(); //closing RL and returning from function.
log('Got it! Your answer was: "', answer, '"');
recursiveAsyncReadLine(); //Calling this function again to ask new question
});
};
recursiveAsyncReadLine(); //we have to actually start our recursion somehow
The key is to not to use synchronous loops. We should ask next rl.question
only after handling answer. Recursion is the way to go. We define function that asks the question and handles the answer and then call it from inside itself after answer handling. This way we starting all over, just like with regular loop. But loops don't care about ansyc code, while our implementation cares.
Here's a version using async generator function:
async function* questions(query: string) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
for (;;) {
yield new Promise((resolve) => rl.question(query, resolve));
}
} finally {
rl.close();
}
}
Which can be used with for await...of like this:
async function run() {
for await (const answer of questions("Command: ")) {
console.log(`I heard ${answer}`);
if (answer == "done") break;
}
}
run(); // For the sake of example, start the async function at the top level of nodejs script