简体   繁体   English

如何让 rl (或提示)返回用户输入的值

[英]How can I make rl (or prompts) return the value of what the user inputs

How can I get rl.question to return the value that the user inputs, my code looks like this:如何让 rl.question 返回用户输入的值,我的代码如下所示:

 const readline = require('readline'); const { stdin: input, stdout: output } = require('process'); const rl = readline.createInterface({ input, output }); console.log(rl.question("hello?",(answer) => {return answer}))

I know there are probably better modules to use if I want a python-style input function, but 'prompts' seems to have the same problem.我知道如果我想要一个 python 样式的输入 function 可能有更好的模块可以使用,但是“提示”似乎有同样的问题。

I'm not super sure what you are asking, so let me know if this isn't it.我不太确定你在问什么,所以如果不是这样,请告诉我。

ReadLine Docs ReadLine 文档

To console.log() the answer from a ReadLine question there are multiple methods, here are some.console.log()来自 ReadLine 问题的答案,有多种方法,这里有一些。

try moving the console.log inside of the {}尝试将 console.log 移动到 {}

rl.question('hello? ', (answer) => {
 console.log(answer);
 // ... whatever you want to do with the answer variable 
})

or you can use async/await with promise或者您可以将async/awaitpromise一起使用

async function AskQuestion() {
  return new Promise(async (resolve, reject) => {
    rl.question('Hello? ', (answer) => {
      resolve(answer);
    });
  });
}

and then just await the function somewhere然后在某处等待 function

(async () => {
    let answer = await AskQuestion();
    console.log(answer);
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户输入“否”时,如何结束此过程 - How can I make this process end when user inputs “no” 我如何通过使用自定义输入获取用户输入的行数和列数来使用js在网站上制作表格 - How can i make a table on a website using js by taking user inputs of number of rows and columns with custom inputs 我如何将我的 else/if 返回放在带有用户提示输入值的网页上 - how do I put my else/if return on a web page with user prompts for values 如何使输入显示内联? - How can I make inputs display inline? 如何返回用户输入的数据? - How to return data that user inputs? 如果用户输入错误的值,如何在货币转换器中输出错误消息? - How can I output an error message in my currency converter if the user inputs wrong value? 如何让 Javascript 控制台不打印函数的返回值 - How can I make the Javascript console NOT print the return value of a function 如何使用 Jquery 从用户输入制作表格内容? - How I could make a table content from user inputs with Jquery? 如何限制可以在提示框中输入的单词,每个单词给出不同的提示? - How do I limit what words can be put into a prompt box to give different prompts per each word? 如何根据用户请求生成多个输入? - How can I generate multiple inputs upon user's request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM