Dan D Kim

Let's share stories

Promise.all vs Promise.allSettled

2019-08-26 Dan D. Kimjavascript

Let’s say you had a bunch of promises to run in parallel and get the results for all of them.

const p1 = new Promise(resolve => resolve('Dan'))
const p2 = new Promise((_, reject) => reject(Error('Uh oh')))
const p3 = new Promise(resolve => resolve(100))

Promise.all will short-circuit when any of the promises fail.

Promise.all([p1, p2, p3])

  .then(response => console.log(response))

  .catch(error => console.log(error)) // Error: Uh oh

Promise.allSettled will not short-circuit, and wait for all promises to settle.

Promise.allSettled([p1, p2, p3])

  .then(response => console.log(response))

  // 0: {status: "fulfilled", value: "Dan"}
  // 1: {status: "rejected", reason: Error: Uh oh 
  // 2: {status: "fulfilled", value: 100}

That’s it. Hope you learned something new.

If you would like to know more, here is the original proposal for Promise.allSettled - Github link