What is the difference between Promise all and allSettled methods with examples

This tutorial explains the differences between Promise.all() and Promise.allSettled() with examples.

A promise is a JavaScript object used for asynchronous operations. It is employed to execute future jobs that can have statuses:

  • pending: Default initial state
  • fulfilled: The task is successfully completed with a value
  • rejected: Task failed with a reason

Promises were introduced in ES6 version of JavaScript.

Promise.all() Method JavaScript Example

Promise.all() accepts iterable promises and returns an array of the results of promises:

  • Result is rejected when one of the array promises is rejected.
  • Result is resolved with an array of data when all promises are resolved.
Promise.all(iterable promises)

Iterable promises are an array of promises. If one of the promises is rejected, Promise.all() rejects with a rejection.

Let’s see different use cases with examples:

  • All promises are resolved
Promise.all([Promise.resolve("Success"), Promise.resolve("Success"),
Promise.resolve("Success")
])
.then((data) => console.log("result", data)).catch((error) =>
console.log("error", error))

Output:

result ["Success", "Success", "Success"]
  • One of the promises is rejected:

all method takes three promises. One promise is rejected, 2 processes are resolved and the result is failure.

Promise.all([Promise.reject("failure"), Promise.resolve("Success"),
Promise.resolve("success")
])
.then((data) => console.log("result", data)).catch((error) =>
console.log("error", error))
error failure
  • All the promises are rejected:

If all promises are rejected, the First promise executed is returned with rejected error

Promise.all([Promise.reject("p1 failed"), Promise.reject("p2 failed"),Promise.reject("p3 failed")])
.then((data)=>console.log("result",data)).catch((error)=>console.log("error",error))
error
p1 failed

Promise.allSettled JavaScript Example

Promise.allSettled() accepts iterable promises and returns an array of the result of promises with both rejected and resolved status.

All the promises are settled with either reject or resolved and return an object.

  • If the promise is fulfilled, it returns an object with status=fulfilled and value.
  • If the promise is rejected, it returns an object with status=rejected and reason.
Promise.all(iterable promises)
  • All promises are resolved In this example, all promises are resolved
Promise.allSettled([Promise.resolve("success"), Promise.resolve("Success"), Promise.resolve("success")])
.then((data) => console.log(data)).catch((error) => console.log("error", error))

Output:

[
  { status: "fulfilled", value: "success" },
  { status: "fulfilled", value: "success" },
  { status: "fulfilled", value: "success" }
]
  • One of the promises is rejected

In this example one of the promises is rejected, remaining promises are resolved.

Promise.allSettled([Promise.resolve("success"), Promise.reject("failed"),Promise.resolve("success")])
.then((data)=>console.log(data)).catch((error)=>console.log("error",error))

Output:

[
  { status: "fulfilled", value: "success" },
  { status: "rejected", reason: "failed" },
  { status: "fulfilled", value: "success" }
]
  • All the promises are rejected
Promise.allSettled([Promise.reject("p1 failed"), Promise.reject("p2 failed"),Promise.reject("p3 failed")])
.then((data)=>console.log("result",data)).catch((error)=>console.log("error",error))

Output:

[
  { status: "rejected", value: "p1 failed" },
  { status: "rejected", reason: "p2 failed" },
  { status: "fulfilled", value: "p3 failed" }
]

What is the difference between promise all and Promise allSettled

Promise.allPromise.allSettled
Takes iterable array promisesTakes iterable array promises
Returns result of all promises when all resolvedReturns result of all promises when all resolved
If one promise is rejected, it rejects as a rejectIt returns the result of all promises that are settled with either resolved or rejected
Returns an array of data when all promises are resolvedReturns an array of objects where each object contains status and data
Returns rejected message when one promise is rejectedReturns an array of objects where each object contains status and reason for rejected promise
Both are not parallelNot Parallel