A simple yes/no script yields weird behavior

2019-07-18 12:42发布

I am trying to implement a simple yes/no script.

A function runs, then the user is prompt to input whether or not to run it again.

If the user inputs "y", then the procedure should repeat.

If the user inputs "n", then the procedure should terminate.

If neither, then the question should repeat.

Here is my code:

function func(i) {
    console.log(i);
    ask(i + 1);
}

function ask(i) {
    process.stdout.write("again (y/n)?");
    process.stdin.on("data", function(data) {
        process.stdin.end();
        if (data.toString().trim() == "y")
            func(i);
        else if (data.toString().trim() != "n")
            ask(i);
    });
}

func(0);

Unfortunately, the process always terminates at the second time the question is asked.

I tried removing the process.stdin.end() part, and I got a really weird behavior:

First time I input "y", the question is asked once and the function runs once.

Second time I input "y", the question is asked twice and the function runs twice.

Third time I input "y", the question is asked thrice and the function runs thrice.

In addition, at some point I start getting this error:

(node:12336) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit

Can anybody please shed some light on what's going on here?

Any suggestions on how to make this work will be very much apprteciated.

Thank you!!!

1条回答
Melony?
2楼-- · 2019-07-18 13:05

The line

process.stdin.end();

prevents further input, and (here) effectively ends the program. So, you'll want to call it only once, once everything is done. The

process.stdin.on

command adds a listener to when the user presses "enter". If you call this multiple times, multiple listeners will be added.

So, you might consider adding just one listener, calling process.stdin.end(); if the response is n:

let i = 0;
function func() {
  console.log(i);
  i++;
  ask();
}
process.stdin.on("data", function(data) {
  if (data.toString().trim() === "y")
    func(i);
  else if (data.toString().trim() === "n")
    process.stdin.end();
  else
    ask();
});
function ask() {
  process.stdout.write("again (y/n)?");
}

func();

Output example:

PS D:\Downloads> node foo.js
0
again (y/n)?
again (y/n)?
again (y/n)?y
1
again (y/n)?y
2
again (y/n)?
again (y/n)?y
3
again (y/n)?n
PS D:\Downloads>
查看更多
登录 后发表回答