codeapp
codeapp copied to clipboard
Readline not working as expected in NodeJS
Hi! First, many thanks for supporting process.stdin
in nodejs! This was really a feature I was waiting for! :-)
Please note that the sample code below does not work as expected though, as the program hangs (waiting for further input) and does not exit after input as it should. rl.close()
should indeed close the interface and let the program close.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Username: ', user => {
console.log(`Hello ${user}`);
rl.close();
});
Kind regards, Maurice
Thanks for testing the feature! I can confirm this issue and will look into it. As a workaround, it is possible to end the program by pressing command-c on the keyboard (or its button).
Update: Not quite sure what caused this but another workaround I found is setTimeout(() => {rl.close()})
.