JavaScript continues to evolve with exciting new features. While proposals like the pipe operator (similar to F#'s elegant function composition) are still in development, I want to highlight a powerful feature that's already available: Promise.allSettled()
.
When working with multiple promises, developers often reach for Promise.all()
. However, this method takes an "all or nothing" approach. Let's understand why this might not always be ideal.
Understanding Promise.all()
Promise.all()
works as follows:
- Takes an array of promises and returns a new promise
- Resolves only when ALL promises resolve successfully
- Rejects immediately if ANY promise rejects
- Returns an array of resolved values in the same order as input promises
Here's a practical example:
Promise.allSettled()
While Promise.all()
is useful for scenarios where you need all promises to succeed, Promise.allSettled()
takes a more forgiving approach.
It waits for all promises to complete, regardless of whether they succeed or fail. This makes it particularly valuable when you need to:
- Track the outcome of multiple independent operations
- Handle partial successes gracefully
- Collect both successful and failed results
Here's how Promise.allSettled()
works:
The key difference is in the result format. Each promise outcome is represented by an object containing:
- A
status
field: either 'fulfilled' or 'rejected' - A
value
field for successful promises - A
reason
field for rejected promises
Real-World Example
Let's look at a practical scenario where Promise.allSettled()
shines, calling some user data APIs:
In this example, even if some API calls fail, we can still:
- Process the successfully fetched user data
- Log or handle the failed requests separately
- Provide meaningful feedback about partial successes
Browser Support
Promise.allSettled()
is well-supported in modern browsers and can be polyfilled for older environments. It's part of ES2020 and is available in:
- Chrome 76+
- Firefox 71+
- Safari 13+
- Edge 79+
- Node.js 12.9.0+
But all depends when you are reading this blog post, so make sure to check the latest compatibility tables on MDN. Perhaps it is already available in all major browsers!
When to Use What
-
Use
Promise.all()
when:- You need all operations to succeed
- A single failure should stop the entire process
- You're dealing with dependent operations
-
Use
Promise.allSettled()
when:- You want to handle partial successes
- Operations are independent of each other
- You need detailed feedback about each promise's outcome
Summery
Use Promise.allSettled()
when you need to handle multiple promises independently and want to track their outcomes, whether they succeed or fail.
It's a powerful tool that provides more flexibility than Promise.all()
in scenarios where partial successes are acceptable.
Additional Resources
-
MDN Web Docs:
- Promise.allSettled() - Comprehensive documentation with examples
- Promise.all() - For comparison and understanding differences
-
JavaScript Specifications:
- TC39 Proposal for Promise.allSettled - The original proposal with detailed explanations
- ECMAScript 2020 Specification - Official specification