admin管理员组文章数量:1406951
My app code:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function init() {
while (true) {
console.log("TICK");
await (rl.question('What do you think of Node.js? ', await (answer) => {
console.log('Thank you for your valuable feedback:', answer);
rl.close();
}))
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
How it must work (or how i think it should work):
When we meet await (rl.question('...
it should wait for the response (user input) and only than loop continue.
How it actually works
When it meets await new Promise(resolve => setTimeout(resolve, 1000))
it's working, but with await (rl.question('...
you get output but code continue executing without waiting for user input.
My app code:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function init() {
while (true) {
console.log("TICK");
await (rl.question('What do you think of Node.js? ', await (answer) => {
console.log('Thank you for your valuable feedback:', answer);
rl.close();
}))
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
How it must work (or how i think it should work):
When we meet await (rl.question('...
it should wait for the response (user input) and only than loop continue.
How it actually works
When it meets await new Promise(resolve => setTimeout(resolve, 1000))
it's working, but with await (rl.question('...
you get output but code continue executing without waiting for user input.
-
Is
while
loop necessary? – guest271314 Commented Jul 31, 2016 at 0:06 - @guest271314 , Yes. I want to understand why some async work and some don't. Like in my case – Src Commented Jul 31, 2016 at 0:31
2 Answers
Reset to default 3async
functions require a function that returns a promise. rl.question
doesn't return a promise; it takes a callback. So you can't just stick async
in front of it an hope it will work.
You can make it work by wrapping it in a promise, but this is probably more work than it's worth:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function rl_promise(q) {
return new Promise(resolve => {
rl.question('What do you think of Node.js? ', (answer) => {
resolve('Thank you for your valuable feedback:', answer)
})
})
}
async function init() {
while (true) {
console.log("TICK");
let answer = await rl_promise('What do you think of Node.js? ')
console.log(answer)
}
rl.close();
}
init()
Having said that, a better approach is to avoid the while loop and have a stopping condition. For example, when the user types 'quit'. I think this is simpler and much easier to understand:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask() {
rl.question('What do you think of Node.js? ', (answer) => {
console.log('Thank you for your valuable feedback:', answer);
if (answer != 'quit') ask()
else rl.close();
})
}
ask()
While loop is not async. It is needed to use an async function as iteratee. You can find more info here:
https://medium./@antonioval/making-array-iteration-easy-when-using-async-await-6315c3225838
Personally I've used Promise.map of bluebird.
本文标签: javascriptAwait not working in while loopStack Overflow
版权声明:本文标题:javascript - Await not working in while loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744909754a2631841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论