user13406409
user13406409

Reputation:

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...");

Upvotes: 0

Views: 59

Answers (1)

Quentin
Quentin

Reputation: 943615

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.

Upvotes: 1

Related Questions