最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Process never ends when using 'process.stdin.once' - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 7, 2020 at 12:00 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jan 14, 2019 at 13:45 goodvibrationgoodvibration 6,2064 gold badges34 silver badges68 bronze badges 4
  • That cannot be all of your code, as await can only be use inside an async function. – Pointy Commented Jan 14, 2019 at 13:55
  • @Pointy: Of course it's not all of my code. I've pretty much stated that explicitly ("my script does this and that..."). And yes - this snippet is indeed invoked from an async function. – goodvibration Commented Jan 14, 2019 at 13:56
  • OK well one never knows around here what might be lurking in unposted code. That said, the given answer is correct, though idiomatically in UNIX/Linux C code you'd probably just explicitly exit() or fall out of the main function (which I realize doesn't exist in Node programs). – Pointy Commented Jan 14, 2019 at 14:18
  • 1 @Pointy: Technically, since I do know where my script starts and ends looking past all the asynchronous calls), I just ended up adding process.stdin.resume() at the beginning and process.stdin.pause() at the end. All the rest remained the same. Thank you. – goodvibration Commented Jan 14, 2019 at 14:25
Add a ment  | 

1 Answer 1

Reset to default 12

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();
});
发布评论

评论列表(0)

  1. 暂无评论