In my node script, I am waiting for the user to press enter at some point:
console.log("Press enter to continue...");
await new Promise(function(resolve, reject) {
process.stdin.once("data", function(data) {
resolve();
});
});
The script runs fine, and continues only after the user has pressed enter.
But at the end of the execution, the process does not terminate.
Instead, it seems to be just pending for user input (i.e., a newline is printed every time I press enter).
I'm pretty sure that the problem is related to process.stdin.once
, and not to how I use the Promise
in order to force synchronous execution.
I have several places in my script where I have to wait for the user to press enter, so adding process.stdin.end()
before resolving the promise is out of the question here (it means that waiting for the user to press enter will work only once).
How can I resolve this problem?
await
can only be use inside anasync
function. – Snowonthemountainasync
function. – Lowestoftexit()
or fall out of themain
function (which I realize doesn't exist in Node programs). – Snowonthemountainprocess.stdin.resume()
at the beginning andprocess.stdin.pause()
at the end. All the rest remained the same. Thank you. – Lowestoft