Learnitweb

Understanding Promise.allSettled() in JavaScript

In the previous lessons, we discussed Promise.all() and how it works when executing multiple asynchronous operations in parallel.

You also saw an important limitation:

If any one promise rejects, Promise.all() immediately rejects and stops waiting for the remaining promises.

In this tutorial, we’ll explore a safer alternative — Promise.allSettled()

This method allows you to run multiple promises without failing the entire operation, even if some of them reject.

Quick Recap: The Problem with Promise.all()

Earlier, we had three functions representing three car dealers:

  • Dealer 1 → resolves with a price
  • Dealer 2 → may reject
  • Dealer 3 → resolves with a price

When we used Promise.all():

  • If any dealer rejected, the entire result failed.
  • We had to manually add .catch() to each promise to prevent early failure.

While that works, JavaScript provides a cleaner and more expressive solution.

Introducing Promise.allSettled()

Promise.allSettled():

  • Accepts an array of promises
  • Returns one promise
  • That promise never rejects
  • It always resolves with a detailed result of every promise

This makes it ideal when:

  • You want results from all operations
  • Failures are acceptable
  • You still want visibility into what failed and what succeeded

Using Promise.allSettled()

Let’s replace Promise.all() with Promise.allSettled().

Promise.allSettled([
    dealerOne(),
    dealerTwo(),
    dealerThree()
]).then(results => {
    console.log(results);
});

What Does the Result Look Like?

Instead of returning plain values, Promise.allSettled() returns an array of objects.

Each object describes the outcome of a promise.

Example output:

[
  { status: "fulfilled", value: 8000 },
  { status: "rejected", reason: "Not a suitable car" },
  { status: "fulfilled", value: 10000 }
]

Understanding the Structure

Each result object contains:

1. status

This tells whether the promise was:

  • "fulfilled"
  • "rejected"

2. value or reason

  • If the promise fulfilled, you get: { status: "fulfilled", value: <result> }
  • If the promise rejected, you get: { status: "rejected", reason: <error> }

This structure allows you to process both success and failure safely.

How This Differs from Promise.all()

FeaturePromise.all()Promise.allSettled()
Rejects if one fails✅ Yes❌ No
Always resolves❌ No✅ Yes
Useful for critical operations✅ Yes⚠️ Not ideal
Useful when partial failure is acceptable❌ No✅ Yes
Return formatArray of valuesArray of result objects

Using Promise.allSettled() with Non-Promise Values

Just like Promise.all(), this method also accepts regular values.

Promise.allSettled([1, "hello", true]).then(results => {
    console.log(results);
});

Output:

[
  { status: "fulfilled", value: 1 },
  { status: "fulfilled", value: "hello" },
  { status: "fulfilled", value: true }
]

Even non-promises are treated as successfully resolved values.

What Happens with an Empty Array?

Promise.allSettled([]).then(result => {
    console.log(result);
});

Output:

[]

The promise:

  • Resolves immediately
  • Returns an empty array

When Should You Use Promise.allSettled()?

You should use Promise.allSettled() when:

  • You want all results, regardless of success or failure
  • One failure should not stop the entire process
  • You want to inspect results individually
  • You are building dashboards, reports, or batch operations

Example Use Cases:

  • Fetching data from multiple APIs where partial data is acceptable
  • Running background tasks where failure isn’t critical
  • Collecting analytics from multiple services

When Should You Use Promise.all() Instead?

Use Promise.all() when:

  • All operations are required to succeed
  • Failure of one means the whole operation must stop
  • You want fast failure and strict validation