0
0
JavascriptHow-ToBeginner · 3 min read

How to Use Promise.allSettled in JavaScript: Syntax and Examples

Use Promise.allSettled to wait for all promises to finish, whether they resolve or reject. It returns a new promise that resolves with an array of objects describing each promise's outcome.
📐

Syntax

The Promise.allSettled method takes an iterable (usually an array) of promises and returns a new promise. This new promise resolves when all input promises have settled (either fulfilled or rejected). The resolved value is an array of objects, each describing the outcome of each promise.

  • promises: An array or iterable of promises.
  • Returns: A promise that resolves to an array of result objects.
  • Each result object has a status property: "fulfilled" or "rejected".
  • If status is "fulfilled", the object has a value property with the resolved value.
  • If status is "rejected", the object has a reason property with the error.
javascript
Promise.allSettled(iterableOfPromises).then(results => {
  // results is an array of {status, value} or {status, reason}
});
💻

Example

This example shows how Promise.allSettled waits for all promises to finish and then logs their results, including both successes and failures.

javascript
const promise1 = Promise.resolve('Success 1');
const promise2 = Promise.reject('Error 2');
const promise3 = new Promise(resolve => setTimeout(() => resolve('Success 3'), 100));

Promise.allSettled([promise1, promise2, promise3])
  .then(results => {
    results.forEach((result, index) => {
      if (result.status === 'fulfilled') {
        console.log(`Promise ${index + 1} fulfilled with:`, result.value);
      } else {
        console.log(`Promise ${index + 1} rejected with:`, result.reason);
      }
    });
  });
Output
Promise 1 fulfilled with: Success 1 Promise 2 rejected with: Error 2 Promise 3 fulfilled with: Success 3
⚠️

Common Pitfalls

One common mistake is using Promise.all when you want to handle all results regardless of failures. Promise.all rejects immediately on the first failure, while Promise.allSettled waits for all promises.

Another pitfall is not checking the status property of each result, which can cause errors if you assume all promises succeeded.

javascript
/* Wrong: Using Promise.all rejects on first error */
const p1 = Promise.resolve('OK');
const p2 = Promise.reject('Fail');

Promise.all([p1, p2])
  .then(results => console.log('All succeeded:', results))
  .catch(error => console.log('Promise.all rejected:', error));

/* Right: Using Promise.allSettled handles all results */
Promise.allSettled([p1, p2])
  .then(results => {
    results.forEach(r => {
      if (r.status === 'fulfilled') {
        console.log('Fulfilled:', r.value);
      } else {
        console.log('Rejected:', r.reason);
      }
    });
  });
Output
Promise.all rejected: Fail Fulfilled: OK Rejected: Fail
📊

Quick Reference

FeatureDescription
InputAn iterable (usually array) of promises
OutputA promise resolving to an array of result objects
Result ObjectHas 'status' ('fulfilled' or 'rejected')
If fulfilledResult object has 'value' property
If rejectedResult object has 'reason' property
Use caseGet all results without failing fast on errors

Key Takeaways

Promise.allSettled waits for all promises to finish, regardless of success or failure.
It returns an array of objects describing each promise's outcome with status and value or reason.
Use Promise.allSettled when you want to handle all results without stopping at the first error.
Always check the status property of each result before using its value or reason.
Promise.all rejects immediately on the first failure, unlike Promise.allSettled.