Promise.all, allSettled, race, and any
Promise.all waits for every input to fulfill and rejects as soon as one rejects. Promise.allSettled waits for every input and reports each outcome. Promise.race settles when the first input settles. Promise.any fulfills when the first input fulfills and rejects only when every input rejects.
Choosing the correct combinator is about business semantics, not just syntax.
Parallel Independent Requests
const [user, posts] = await Promise.all([
fetch('/api/user').then(r => r.json()),
fetch('/api/posts').then(r => r.json())
]);
console.log(user, posts);💡 Both requests begin without waiting for each other. Promise.all waits for both successful results.
Key Takeaways
- Promise.all is appropriate when every operation must succeed.
- Promise.allSettled is useful when every result matters regardless of success.
- Promise.any is useful when any successful source is sufficient.
Knowledge Check
Which Promise combinator waits for all inputs regardless of whether they fulfill or reject?