Promise.all vs Promise.allSettled in JavaScript: Key Differences and Usage
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.
| Factor | Promise.all | Promise.allSettled |
|---|---|---|
| Behavior on rejection | Rejects immediately on first promise rejection | Waits for all promises to settle, no immediate rejection |
| Result type | Array of resolved values | Array of objects with status and value/reason |
| Use case | When all promises must succeed | When you want all results regardless of success or failure |
| Error handling | Single catch for any failure | Individual results show success or failure |
| Introduced in | ES2015 (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.
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); });
Promise.allSettled Equivalent
This example shows how Promise.allSettled handles the same promises, reporting all results.
const promises = [ Promise.resolve('Success 1'), Promise.reject('Error occurred'), Promise.resolve('Success 2') ]; Promise.allSettled(promises) .then(results => { console.log('All results:', results); });
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.Promise.all for fail-fast scenarios where all promises must succeed.Promise.allSettled to handle multiple outcomes without stopping on errors.