ES2020 - Promise.allSettled

ยท 1 mins read ยท ย JavaScript

Promise.allSettled() is a method which returns a Promise that gets resolved when all of the given Promises have been resolved or rejected.

We can pass an array of Promises we want to keep track of:

const result = await Promise.allSettled([
  new Promise(resolve => setTimeout(() => resolve(66), 0)),
  Promise.reject(new Error('an error'))

The result is equal with:

  {status: "fulfilled", value: 33},
  {status: "fulfilled", value: 66},
  {status: "fulfilled", value: 99},
  {status: "rejected",  reason: "Error: an error"}

Notice that for each Promise an object is provided in the end when Promise.allSettled() gets resolved. Each object contains a key named status. Its value can be fulfilled or rejected.

When it is equal with fullfilled, another key named value with the resolved outcome is provided. On the other hand, when status is equal with rejected, then a key named reason is added instead of value. This key contains some details about the reason this Promise didn't resolve.

The difference with Promise.all() is that the latter gets resolved when all Promises get successfully resolved. While Promise.race() gets resolved when at least one of the given Promises has been resolved or rejected.

Promise.allSettled() method will help us to build applications with progressive enhancement. Cheers!!

At the moment of this writing, Promise.allSettled() is in stage 4 of the process which is considered finished

We can use it with the help of core-js

Did you like this one?

I hope you really did...

Buy Me A Coffee


Get notified about latest posts and updates once a week!!


Buy Me A Coffee