Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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
Node.js 在nodejs中接受输入_Node.js - Fatal编程技术网

Node.js 在nodejs中接受输入

Node.js 在nodejs中接受输入,node.js,Node.js,我从点头开始。这是我的密码 function ask(callback) { var stdin = process.stdin, stdout = process.stdout; stdin.resume(); stdin.once('data', function(data) { data = data.toString().trim(); callback(data); }); } ask(function(testcase

我从点头开始。这是我的密码

function ask(callback) {
    var stdin = process.stdin, stdout = process.stdout;
    stdin.resume();
    stdin.once('data', function(data) {
        data = data.toString().trim();
        callback(data);
    });
}

ask(function(testcase) {
    ask(function(workers) {
        ask(function(salary) {
            console.log(salary);
        });
    });

});
我想做的是基于第一个输入,即testcase,我想围绕两个ask(函数(workers))和ask(函数(salary))运行一个for循环

现在开始处理输入。我知道nodeJS是aysnchronus,因此即使我在第一个ask(函数(testcase))之后放置for循环也是如此。它不起作用


有谁能指导我如何以同步方式接受输入

我会这样做:

function test(testcase) {
    ask(function(workers) {
      ask(function(salary) {
        console.log(salary);

        // Do whatever you want with the data

        // Then:
        ask(test);
      });
    });
}

ask(test);
标准输入没有同步IO API。但是,您可以将整个输入读入一个字符串(直到出现EOF),然后以同步方式处理该字符串

function test(testcase) {
    ask(function(workers) {
      ask(function(salary) {
        console.log(salary);

        // Do whatever you want with the data

        // Then:
        ask(test);
      });
    });
}

ask(test);