DEV Community

Discussion on: How to use async/await with .map in js

Collapse
 
thuutri2710 profile image
Tris

Agree with you @jordan Jaramillo. Btw, we can also use Promise.all to resolve this problem. Assume that we're fetching many requests. If one of them is successful, we'll return:

{
  value: 20,
  status: success
}
Enter fullscreen mode Exit fullscreen mode

Otherwise

{
 value: null,
 status: failed
}
Enter fullscreen mode Exit fullscreen mode

How about your thought?

Thread Thread
 
jordandev profile image
Jordan Jaramillo

The problem with promise.all is that you will not be able to access which ones were resolved correctly since it directly passes to the catch.

Thread Thread
 
thuutri2710 profile image
Tris

I mean that if any request is failed, we still use resolve instead of reject. We only change the returned status.

{
  value: null,
  status: failed,
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
jordandev profile image
Jordan Jaramillo

yes, you can do it that way, it's a good idea I hadn't thought about it but if you want to have it automatically you can use allSettled.

Thread Thread
 
thuutri2710 profile image
Tris

Agree !!!