Node.js 无限循环功能,在某些用户输入时退出

Bry*_*yan 6 javascript node.js

我不太了解节点在异步和循环方面的工作原理。我想在这里实现的是让控制台打印出“命令: ”并等待用户的输入。但是在等待时,我希望它无休止地运行“ someRandomFunction() ”,直到用户在终端上输入“exit”。

非常感谢所有帮助 - 可能还有一个解释,以便我理解!

谢谢!:)

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("Command: ", function(answer) {
    if (answer == "exit"){
        rl.close();
    } else {
        // If not "exit", How do I recall the function again?
    }
});

someRandomFunction();
Run Code Online (Sandbox Code Playgroud)

Pab*_*mer 9

我建议像这样使函数可重复。

var rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});

var waitForUserInput = function() {
  rl.question("Command: ", function(answer) {
    if (answer == "exit"){
        rl.close();
    } else {
        waitForUserInput();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

然后打电话

waitForUserInput();
someRandomFunction();
Run Code Online (Sandbox Code Playgroud)

不过,我不确定您用于 .question 的语法是否正确,那部分代码是否有效?

您也可以按以下方式编写。

var rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});

function waitForUserInput() {
  rl.question("Command: ", function(answer) {
    if (answer == "exit"){
        rl.close();
    } else {
        waitForUserInput();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

这里的重要教训是,要重用一个函数,它必须被命名并且在作用域中可用。如果您对此还有任何疑问,请提问。

  • 请原谅菜鸟的问题,但这不是通过不断调用自身来不断添加到堆栈中吗? (2认同)

And*_*w E 8

另一个答案很好,但不必要地使用递归。

解决这个问题的关键是,在您看来,将其他语言中使用的简单的基于循环的方法与 Node.js 的异步方法分开。

在其他语言中,您可以使用如下循环:

while not finished:
  line = readline.read() 
  if line == 'woof':
    print('BARK')
  elif line == 'exit':
    finished = True
  ... # etc
Run Code Online (Sandbox Code Playgroud)

Node,至少对于 Readline,不是这样工作的。

在 Node 中,您启动 Readline,为其提供事件处理程序,然后返回,并稍后处理 readline 循环的完成。

考虑以下代码,您可以复制粘贴运行:

const readline = require('readline');

function replDemo() {
  return new Promise(function(resolve, reject) {
    let rl = readline.createInterface(process.stdin, process.stdout)
    rl.setPrompt('ready> ')
    rl.prompt();
    rl.on('line', function(line) {
      if (line === "exit" || line === "quit" || line == 'q') {
        rl.close()
        return // bail here, so rl.prompt() isn't called again
      }

      if (line === "help" || line === '?') {
        console.log(`commands:\n  woof\n  exit|quit\n`)
      } else if (line === "woof") {
        console.log('BARK!')
      } else if (line === "hello") {
        console.log('Hi there')
      } else {
        console.log(`unknown command: "${line}"`)
      }
      rl.prompt()

    }).on('close',function(){
      console.log('bye')
      resolve(42) // this is the final result of the function
    });
  })
}

async function run() {
  try {
    let replResult = await replDemo()
    console.log('repl result:', replResult)

  } catch(e) {
    console.log('failed:', e)
  }
}

run()
Run Code Online (Sandbox Code Playgroud)

运行此命令,您将得到如下输出:

$ node src/repl-demo.js
ready> hello
Hi there
ready> boo
unknown command: "boo"
ready> woof
BARK!
ready> exit
bye
repl result: 42
Run Code Online (Sandbox Code Playgroud)

请注意,该run函数调用replDemo并“等待”promise 的结果。

如果您不熟悉 async/await,以下是用“传统”Promise 风格编写的相同逻辑:

function run2() {
  replDemo().then(result => {
    console.log('repl result:', result)
  }).catch(e => {
    console.log('failed:', e)
  })
  console.log('replDemo has been called')
}
Run Code Online (Sandbox Code Playgroud)

请注意,我添加输出“replDemo 已被调用”是有原因的 - 运行上面的命令会显示如下输出:

$ node src/repl-demo.js
ready> replDemo has been called
woof
BARK!
ready> hello
Hi there
ready> bye
repl result: 42
Run Code Online (Sandbox Code Playgroud)

请注意“replDemo 已被调用”是如何在第一个“ready>”提示之后立即出现的。这是因为replDemo()函数立即返回,然后run2()立即退出,一切main都完成了 - 但 readline 仍在执行!

如果您像我一样具有命令式编程背景,那么这一点很难理解。Nodejs 核心的异步事件驱动循环一直运行,直到所有工作完成,这发生在最后一个 Promise 被解决时,这发生在 readline 实例“关闭”时,这发生在输入“退出”时用户(或收到 EOF,在大多数系统上为 CTRL+D,在 Windows 上为 CTRL+Z)。