简体   繁体   中英

Async/Await is awaiting another?

I have async 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"); never runs before two await above? Is it possible last await is returned faster then first and I get this:

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:

Does it mean that JS delays on each await line, awaiting promise result

Yes

and console.log("Do this befor await above"); never runs before two await above?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM