【问题标题】:Wait for user input from readline module (Node.Js)等待来自 readline 模块(Node.Js)的用户输入
【发布时间】:2022-02-13 12:05:02
【问题描述】:

我正在创建一个模块来获得经验并缩短一些代码。我有一段以简化方式使用 readline 的代码,例如var x = arkin.question("How old are you? ");。 Readline 不等待答案。它产生这个:

你几岁了?未定义

代码:

const readline = require('readline');

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

exports.question = function(q){

  var response;

  rl.setPrompt(q);
  rl.prompt();
  rl.on('line', (userInput) => {
    response = userInput;
    rl.close();
  });

  rl.on('close', () => {
    return response;
  });
}

我这样称呼它:

var age = arkin.question("How old are you? ");
console.log(age);

我已尝试使用此代码:

rl.question(q, (userInput) => {
    rl.close;
    response = userInput;
    return response;
});

但我得到了相同的结果。提前感谢您的帮助。

【问题讨论】:

    标签: javascript node.js node-modules readline


    【解决方案1】:

    每当您调用 arki.question 时,它都会注册事件侦听器 .on("line") 和 .on("close") 然后从函数返回。无论您从 .on("close") 事件侦听器question 函数返回什么,都不知道它,因为它不再在调用堆栈上。您可以使用回调或带有async...await 的承诺来获得结果。

    带有回调

    const readline = require('readline');
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    exports.question = function(q , cb ){
    
        var response;
    
        rl.setPrompt(q);
        rl.prompt();
    
        rl.on('line', (userInput) => {
            response = userInput;
            rl.close();
        });
    
        rl.on('close', () => {
            return cb(response);
        });
    };
    

    你这样称呼它

    var age  = arki.question("how old are you? ", resp => {
        console.log(resp);
    });
    

    承诺

    const readline = require('readline');
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    exports.question = function(q){
    
        var response;
    
        rl.setPrompt(q);
        rl.prompt();
    
        return new Promise(( resolve , reject) => {
    
            rl.on('line', (userInput) => {
                response = userInput;
                rl.close();
            });
    
            rl.on('close', () => {
                resolve(response);
            });
    
        });
    
    
    };
    

    你这样称呼它

    arki.question("how old are you? ").then( response => console.log(response) );
    

    ; ( async () => {
        console.log(await arki.question("how old are you? "));
    })();
    

    【讨论】:

    • 好的,现在我明白了,但是这样做和正常使用一样复杂。感谢您的帮助!
    • 你应该把 const r1 = readline.createInterface(...) 放在问题函数中,否则它只能在第一次工作。
    【解决方案2】:
    //global stuff, where we import readline. 
    //But avoid assigning global vars.
        var age;
        var readline = require('readline'), menu;
            
    //define function
        function whatever() = {
        
    //create readline instance and assign it to menu
        menu = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });
            
    //Use the readline.question method, which is what you're looking for.
    //It requires a callback function with a variable for the users input.
        menu.question('How old are you? ', function(input) {
            age = input;
        });
            
        console.log(`You are ${age} years old.`);
    }
            
    //Tell node to run the program
    whatever();
    

    【讨论】:

    • 请解释你的代码。
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 2019-11-23
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多