[英]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.我不太确定你在问什么,所以如果不是这样,请告诉我。
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/await与promise一起使用
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.