What Is the Difference Between Promise.allSettled() and Promise.all()?
Use Promises Like a Pro
One of the most powerful features of JavaScript is its support for promises, which enable developers to write asynchronous code that can handle complex tasks without blocking the main thread.
Two of the most commonly used promise methods in JavaScript are Promise.all() and Promise.allSettled(). While they may seem similar, they actually have some important differences that are worth understanding.
Promise.all() vs. Promise.allSettled()
Promise.all() is a method that takes an array of promises as input and returns a new promise that resolves when all the promises in the array have resolved. The resolved value is an array containing the resolved values of each individual promise. However, if any of the promises in the input array reject, the returned promise immediately rejects with the reason of the first promise that rejects.
On the other hand, Promise.allSettled() also takes an array of promises as input, but returns a new promise that resolves with an array of objects once all the promises in the array have either resolved or rejected. Each object in the returned array represents the outcome of each promise, and has two properties: “status” (either “fulfilled” or “rejected”) and “value” (the resolved value or rejection reason).
So, the key difference between Promise.all() and Promise.allSettled() is how they handle rejection. Promise.all() immediately rejects as soon as any of the input promises reject, whereas Promise.allSettled() waits for all the promises to settle (either resolve or reject) before returning an array of objects representing each promise’s outcome.
When to use them?
When to use each method depends on the use case. If you need all the promises to resolve successfully before proceeding, and don’t care about any rejections, then Promise.all() is the best choice. However, if you need to know the outcome of each promise, regardless of whether they resolve or reject, then Promise.allSettled() is the better option.
Limitations of Promise.all()
One important limitation of Promise.all() is that it will reject as soon as one of the promises passed to it rejects. This means that if you have a long list of promises and one of them fails, all of the other promises will be cancelled and Promise.all() will reject with the error from the failed promise.
This can be a problem if you have a situation where you want to wait for all promises to settle, regardless of whether some of them fail or not. In this case, Promise.allSettled() would be a better choice.
Using Promise.allSettled()
Promise.allSettled() is a relatively new addition to the JavaScript language, and as such, it may not be supported by all browsers or environments. However, it provides a powerful alternative to Promise.all() in situations where you need to wait for all promises to settle, regardless of whether some of them fail or not.
Here is an example:
const promises = [
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(new Error('Error occurred')),
Promise.resolve(3)
];
Promise.allSettled(promises)
.then(results => {
results.forEach(result => {
if (result.status === 'fulfilled') {
console.log(`Promise fulfilled with value: ${result.value}`);
} else {
console.log(`Promise rejected with error: ${result.value.message}`);
}
});
})
.catch(error => console.error(error));
In this example, we are passing an array of promises to Promise.allSettled(), including one promise that intentionally fails with an error. The result of this function is an array of four objects representing the results of each promise, including the error object from the failed promise.
By checking the “status” property of each result object, we can determine whether the promise was resolved or rejected, and handle the result accordingly.
Conclusion
Whether you need to wait for all promises to resolve, or just need to wait for all promises to settle, there is a solution available in the JavaScript language. By understanding the differences between these functions, you can write more robust and reliable code that handles promises in a more sophisticated way.
Thanks for reading. If you enjoyed this article, consider supporting me by becoming a Medium member.