[英]Async/Await is awaiting another?
I have async function:我有异步 function:
const getUser = async (query) => {
const user = await Users.findOne(query);
const feed = await Feeds.findOne({ user: user._id });
console.log("Do this befor await above");
return { user, feed };
};
Does it mean that JS delays on each await line, awaiting promise result and console.log("Do this befor await above");
是不是意味着JS在每条await线上都有延迟,等待promise结果和
console.log("Do this befor await above");
never runs before two await
above?从来没有在上面两个
await
之前运行? Is it possible last await is returned faster then first and I get this:是否有可能最后一个等待比第一个更快返回,我得到了这个:
return {undefined, feed}
? return {undefined, feed}
?
So, code after async calling is not blocking?那么,异步调用后的代码不会阻塞吗?
getUser();
console.log("Code below is not blocking...");
Assuming findOne
actually returns a promise:假设
findOne
实际上返回 promise:
Does it mean that JS delays on each await line, awaiting promise result
是不是意味着JS在每条await线上都有延迟,等待promise结果
Yes是的
and console.log("Do this befor await above");
和 console.log("在上面的等待之前执行此操作"); never runs before two await above?
从来没有在上面两个 await 之前运行?
Yes是的
Is it possible last await is returned faster then first and I get this:
是否有可能最后一个等待比第一个更快返回,我得到了这个:
No. Feeds.findOne
isn't even called until the promise returned by Users.findOne
resolves.不,直到
Feeds.findOne
返回的Users.findOne
解决后,才会调用 Feeds.findOne。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.