Reputation: 8531
I'm using Node's readline
but it works only for the first time.
At the first try everything works well (answer
is logged and readline
is closed) but another time it looks like the readline
is not being closed (answer
is never logged and console is still waiting for my input even if I've already sent it).
I've class in Node.js (using ES6 with Babel and Nodemon) which has readline
in constructor.
class SomeClass
{
constructor(readline) {
this.readline = readline;
}
askPath() {
this.readline.question('Question: ', (answer) => {
console.log(answer);
this.readline.close();
this.askPath();
});
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let someClass = new SomeClass(rl);
someClass.askPath();
Console looks like:
> Question: answer
> answer
> Question: second answer
> I can still write
> nothing happens...
Upvotes: 2
Views: 494
Reputation: 6714
Just don't call the .close()
function, inside of your callback as it closes the entire readline interface
askPath() {
this.readline.question('Question: ', (answer) => {
console.log(answer);
this.askPath();
});
}
As the documentation says:
The rl.close() method closes the readline.Interface instance and relinquishes control over the input and output streams. [source:nodejs.org]
Upvotes: 3