Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js-如何在变量中存储readline应答_Javascript_Node.js_Readline - Fatal编程技术网

Javascript Node.js-如何在变量中存储readline应答

Javascript Node.js-如何在变量中存储readline应答,javascript,node.js,readline,Javascript,Node.js,Readline,我正在node.js中构建一个Yahtzee。我使用下面的代码请求用户输入。答案需要存储在一个变量中。我假设[answer]用于临时存储应答值,但是如何在不改变代码结构的情况下将[answer]输出到数组中呢 基本代码结构: const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.qu

我正在node.js中构建一个Yahtzee。我使用下面的代码请求用户输入。答案需要存储在一个变量中。我假设[answer]用于临时存储应答值,但是如何在不改变代码结构的情况下将[answer]输出到数组中呢

基本代码结构:

const readline = require('readline');

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

rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
  console.log("Will keep dices: ", answer);
  rl.close();
});
var lines;                                 // Added compared to basic code.
const readline = require('readline');
const rl = readline.createInterface({
  input:  process.stdin,
  output: process.stdout
});
rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
  lines.push(answer);                      // Added compared to basic code.
  console.log("Will keep dices: ", answer);
  rl.close();
});
console.log(lines);                        // Added compared to basic code.
将用户输入答案添加到变量中的扩展基本代码结构:

const readline = require('readline');

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

rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
  console.log("Will keep dices: ", answer);
  rl.close();
});
var lines;                                 // Added compared to basic code.
const readline = require('readline');
const rl = readline.createInterface({
  input:  process.stdin,
  output: process.stdout
});
rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
  lines.push(answer);                      // Added compared to basic code.
  console.log("Will keep dices: ", answer);
  rl.close();
});
console.log(lines);                        // Added compared to basic code.

终端的结果:未定义。

这并不是真正的工作原理-对于异步操作(如用户输入),您应该在回调中处理结果,而不是“等待”完成。您可以做的一件事是将代码包装成承诺,如下所示:

const readline = require('readline');

function getDiceAnswer() {
    return new Promise(resolve => {    
        const rl = readline.createInterface({
            input:  process.stdin,
            output: process.stdout
        });
        rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
            resolve(answer);
            console.log("Will keep dices: ", answer);
            rl.close();
        });
    });   
}
这仍然意味着您需要在回调中处理结果:

const lines = [];
getDiceAnswer().then(answer => {
     lines.push(answer);
     console.log(lines);
});
…但您可以使用Javascript async/await表示法使其看起来更好:

async function getAnswers() {
     const diceAnswer = await getDiceAnswer();

     //this line won't execute until the answer is ready
     lines.push(diceAnswer);
}
另一个简单的替代方法是使用类似于
readline sync
的包使操作成为同步操作: