Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does Promise.all() method differs from Promise.allSettled() method in JavaScript?
In this article, you will understand how Promise.all() method differs from the Promise.allSettled() method in JavaScript.
The Promise.all() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises are fulfilled. It rejects immediately when any of the input promises is rejected, with this first rejection reason.
The Promise.allSettled() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises settle (either fulfilled or rejected), returning an array of objects that describe the outcome of each promise.
Promise.all() Example
Let's see how Promise.all() works when all promises resolve successfully:
console.log("Defining three promise values: promise1, promise2 and promise3");
const promise1 = Promise.resolve(1);
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2, 'Promise Two');
});
const promise3 = 3;
console.log("Running Promise.all method on all the three promise values");
Promise.all([promise1, promise2, promise3])
.then((values) => console.log("All resolved:", values));
Defining three promise values: promise1, promise2 and promise3 Running Promise.all method on all the three promise values All resolved: [ 1, 'Promise Two', 3 ]
Promise.all() with Rejection
Now let's see what happens when one promise rejects:
const promise1 = Promise.resolve(1);
const promise2 = Promise.reject("Error in promise2");
const promise3 = Promise.resolve(3);
Promise.all([promise1, promise2, promise3])
.then((values) => console.log("All resolved:", values))
.catch((error) => console.log("Promise.all failed:", error));
Promise.all failed: Error in promise2
Promise.allSettled() Example
Promise.allSettled() waits for all promises to complete, regardless of whether they resolve or reject:
const promise1 = Promise.resolve(1);
const promise2 = Promise.reject("Error in promise2");
const promise3 = Promise.resolve(3);
console.log("Running Promise.allSettled method on all promises");
Promise.allSettled([promise1, promise2, promise3])
.then((results) => {
console.log("All promises settled:");
results.forEach((result, index) => {
console.log(`Promise ${index + 1}:`, result);
});
});
Running Promise.allSettled method on all promises
All promises settled:
Promise 1: { status: 'fulfilled', value: 1 }
Promise 2: { status: 'rejected', reason: 'Error in promise2' }
Promise 3: { status: 'fulfilled', value: 3 }
Key Differences
| Aspect | Promise.all() | Promise.allSettled() |
|---|---|---|
| Behavior on rejection | Fails fast - rejects immediately | Waits for all to complete |
| Return value on success | Array of resolved values | Array of result objects with status |
| Use case | When all must succeed | When you need all results regardless |
When to Use Each
Use Promise.all() when you need all promises to succeed and want to fail fast if any promise rejects. Use Promise.allSettled() when you want to handle both successful and failed promises, getting results from all of them regardless of individual outcomes.
Conclusion
Promise.all() fails fast on any rejection, while Promise.allSettled() waits for all promises to complete. Choose based on whether you need all operations to succeed or want to handle mixed results.
