Understanding JavaScript Promises: Promise.all vs. Promise.allSettled
Vivek Neupane
IT DEPENDS - FullStack Developer | ReactJS | NextJS | Astro | NodeJS | Express | MongoDB | Firebase | AWS | React Native
In JavaScript, promises provide a powerful way to handle asynchronous operations. When working with multiple promises, JavaScript offers two useful methods to manage them in parallel: Promise.all() and Promise.allSettled(). While both methods allow you to execute promises concurrently, they differ significantly in their handling of resolved and rejected promises. Let’s dive into each method, their behavior, and the scenarios where you might use one over the other.
What is a Promise?
A Promise in JavaScript is an object that represents a value that may be available now, in the future, or never. Promises have three states:
Using Promise.all()
Promise.all() is a method that takes an array (or iterable) of promises and returns a single promise. This returned promise fulfills when all the promises in the array fulfill, and rejects if any promise in the array rejects.
Key Characteristics of Promise.all()
Example Code with Promise.all()
const promise1 = new Promise((resolve) => setTimeout(() => resolve('Promise 1 resolved'), 1000));
const promise2 = new Promise((resolve, reject) => setTimeout(() => reject('Promise 2 rejected'), 500));
const promise3 = new Promise((resolve) => setTimeout(() => resolve('Promise 3 resolved'), 2000));
Promise.all([promise1, promise2, promise3])
.then((results) => {
console.log('All promises resolved:', results);
})
.catch((error) => {
console.error('One of the promises rejected:', error);
});
In this example, promise2 rejects after 500 milliseconds. Because of this rejection, Promise.all() immediately rejects with an error, and the other promises are effectively ignored, regardless of their potential resolutions.
Pros and Cons of Promise.all()
领英推荐
Using Promise.allSettled()
Promise.allSettled() is a method introduced in ES2020. Like Promise.all(), it accepts an array of promises and returns a new promise. However, Promise.allSettled() resolves only after all promises have either resolved or rejected. It does not reject upon encountering a rejected promise, unlike Promise.all().
Key Characteristics of Promise.allSettled()
Example Code with Promise.allSettled()
const promiseA = new Promise((resolve) => setTimeout(() => resolve('Promise A resolved'), 1000));
const promiseB = new Promise((resolve, reject) => setTimeout(() => reject('Promise B rejected'), 500));
const promiseC = new Promise((resolve) => setTimeout(() => resolve('Promise C resolved'), 2000));
Promise.allSettled([promiseA, promiseB, promiseC])
.then((results) => {
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(`Promise ${index + 1} resolved with value:`, result.value);
} else {
console.log(`Promise ${index + 1} rejected with reason:`, result.reason);
}
});
});
In this case, Promise.allSettled() waits for all promises to settle. The result is an array with details about each promise's status and value or reason for rejection, allowing us to handle each outcome individually.
Pros and Cons of Promise.allSettled()
When to Use Each
Conclusion
Promise.all() and Promise.allSettled() both offer robust solutions for managing asynchronous operations, each suited to different scenarios. Promise.all() is ideal when a failure should cancel the operation, while Promise.allSettled() provides a more fault-tolerant approach, allowing all tasks to complete and then handle both successes and errors. Understanding these differences empowers developers to create more flexible, reliable code, and to select the best tool for their specific asynchronous needs.