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

This tutorial explains about Promise all() and Promise allSettled() differences with examples A promise is a javascript object used for asynchronous operations. It is used to execute future jobs that contain status pending: Default initial state fulfilled: The task is done successfully with a value rejected: task failed with a reason It is introduced since ES6 Version in javascript.

Promise all() method javascript example

Promise all() accepts iterable promises and returns the Array of the result of promises

  • Result is rejected when one of the array promises is rejected
  • Result is resolved with 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 the Array of the result of promises with reject and resolved status.

All the promises are settled with either reject or resolved and returns object
if the promise is fulfilled, Returns the object of status=fulfilled and value If the promise is rejected, Returns object of 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 are settled with either resolved or rejected
Returns 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 array of objects where each object contains status and reason for rejected promise
Both are not parallelnot parallel