DEV Community

Discussion on: Async Await usage and pitfalls in Array.prototype.map() and chaining

Collapse
 
puruvj profile image
PuruVJ

Thanks for the suggestion. I left in all those extra async awaits to show these functions return promises. Otherwise, I simply remove async and await if the function returns a promise.

const usersData = await Promise.all(
  IDs.map((id) => getUserData(id))
);
Collapse
 
stexx profile image
Stefan Breitenstein

Sure but I talk about the explicit the await, not the async part.

And here is a nice part of async functions:
They results always in a Promise, so you don't have to care about the return statement.
At least with the Promise/await part.

So what i mean

const usersData = await Promise.all(
  IDs.map(async (id) => getUserData(id)) // returns always a promise, no gain to write also await.
);

There is even a eslint rule for that:
eslint.org/docs/rules/no-return-await

I think they explain the benefits and tradeoffs better than me :)