Process never ends when using 'process.stdin.once'

放肆的年华 提交于 2021-01-27 14:30:46

问题


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?


回答1:


This is happening because as long as your stdin stream is open, node assumes you might be waiting for more data. The stream is automatically opened after you read from it in any way. An easy way to inform node you no longer want more input is to call pause(). Here's a simple example:

async function example() {
    console.log("Step 1");
    await new Promise(function(resolve, reject) {
        process.stdin.once("data", function(data) {
            resolve();
        });
    });

    console.log("Step 2");
    await new Promise(function(resolve, reject) {
        process.stdin.once("data", function(data) {
            resolve();
        });
    });

    // more steps...
}

example().then(() => {
    console.log("Done");
    process.stdin.pause();
});


来源:https://stackoverflow.com/questions/54182732/process-never-ends-when-using-process-stdin-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!