A simple yes/no script yields weird behavior

ぐ巨炮叔叔 提交于 2019-12-23 22:27:02

问题


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:


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>


来源:https://stackoverflow.com/questions/54059628/a-simple-yes-no-script-yields-weird-behavior

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