[英]How can I make rl (or prompts) return the value of what the user inputs
如何让 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}))
我知道如果我想要一个 python 样式的输入 function 可能有更好的模块可以使用,但是“提示”似乎有同样的问题。
我不太确定你在问什么,所以如果不是这样,请告诉我。
要console.log()
来自 ReadLine 问题的答案,有多种方法,这里有一些。
尝试将 console.log 移动到 {}
rl.question('hello? ', (answer) => {
console.log(answer);
// ... whatever you want to do with the answer variable
})
或者您可以将async/await与promise一起使用
async function AskQuestion() {
return new Promise(async (resolve, reject) => {
rl.question('Hello? ', (answer) => {
resolve(answer);
});
});
}
然后在某处等待 function
(async () => {
let answer = await AskQuestion();
console.log(answer);
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.