0
0
JavascriptComparisonBeginner · 4 min read

Promise.all vs Promise.allSettled in JavaScript: Key Differences and Usage

In JavaScript, Promise.all waits for all promises to resolve and rejects immediately if any promise fails, while Promise.allSettled waits for all promises to complete regardless of success or failure, returning their results as an array of status objects.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of Promise.all and Promise.allSettled based on key factors.

FactorPromise.allPromise.allSettled
Behavior on rejectionRejects immediately on first promise rejectionWaits for all promises to settle, no immediate rejection
Result typeArray of resolved valuesArray of objects with status and value/reason
Use caseWhen all promises must succeedWhen you want all results regardless of success or failure
Error handlingSingle catch for any failureIndividual results show success or failure
Introduced inES2015 (ES6)ES2020
⚖️

Key Differences

Promise.all takes an iterable of promises and returns a new promise that resolves only when all input promises resolve. If any promise rejects, Promise.all rejects immediately with that error, stopping further processing. This makes it useful when you need all tasks to succeed before continuing.

In contrast, Promise.allSettled waits for all promises to finish, no matter if they resolve or reject. It returns an array of objects describing each promise's outcome with a status of either "fulfilled" or "rejected", along with the corresponding value or reason. This allows you to handle successes and failures individually without stopping the entire process.

Use Promise.all when you want to fail fast if any promise fails, and use Promise.allSettled when you want to know the outcome of all promises regardless of errors.

⚖️

Code Comparison

This example shows how Promise.all behaves when one promise rejects.

javascript
const promises = [
  Promise.resolve('Success 1'),
  Promise.reject('Error occurred'),
  Promise.resolve('Success 2')
];

Promise.all(promises)
  .then(results => {
    console.log('All succeeded:', results);
  })
  .catch(error => {
    console.log('Promise.all rejected with:', error);
  });
Output
Promise.all rejected with: Error occurred
↔️

Promise.allSettled Equivalent

This example shows how Promise.allSettled handles the same promises, reporting all results.

javascript
const promises = [
  Promise.resolve('Success 1'),
  Promise.reject('Error occurred'),
  Promise.resolve('Success 2')
];

Promise.allSettled(promises)
  .then(results => {
    console.log('All results:', results);
  });
Output
All results: [ { status: 'fulfilled', value: 'Success 1' }, { status: 'rejected', reason: 'Error occurred' }, { status: 'fulfilled', value: 'Success 2' } ]
🎯

When to Use Which

Choose Promise.all when your tasks depend on all promises succeeding and you want to stop immediately if any fail. This is ideal for operations where partial success is not acceptable.

Choose Promise.allSettled when you want to run multiple promises and handle each result individually, regardless of success or failure. This is useful for logging, cleanup tasks, or when you want to gather all outcomes without interruption.

Key Takeaways

Promise.all rejects immediately if any promise fails, returning only successful results if all succeed.
Promise.allSettled waits for all promises to finish and returns detailed results for each, success or failure.
Use Promise.all for fail-fast scenarios where all promises must succeed.
Use Promise.allSettled to handle multiple outcomes without stopping on errors.
Both methods help manage multiple asynchronous tasks but serve different needs.