Promise.all, race, allSettled, any
MedJavaScript provides powerful static methods for working with multiple Promises: Promise.all() waits for all to succeed (fails fast), Promise.race() returns the first to settle, Promise.allSettled() waits for all regardless of outcome, and Promise.any() returns the first success. Knowing when to use each is essential for efficient async operations.
Interactive Visualization
CodeSetup
1const p1 = Promise.resolve("A");2const p2 = Promise.resolve("B");3const p3 = Promise.resolve("C");45// Compare all four methods:6Promise.all([p1, p2, p3])7Promise.race([p1, p2, p3])8Promise.allSettled([p1, p2, p3])9Promise.any([p1, p2, p3])
Input Promises
p1
FULFILLED
"A"
1
p2
FULFILLED
"B"
2
p3
FULFILLED
"C"
3
Console Output
-
Three promises created - all immediately fulfilled
1 / 4
Key Insight: When all promises fulfill: race and any return the first value, all returns an array of all values, allSettled returns status objects.
Key Points
- Promise.all([promises]) - waits for all, fails on first rejection
- Promise.race([promises]) - returns first to settle (fulfilled OR rejected)
- Promise.allSettled([promises]) - waits for all, never rejects
- Promise.any([promises]) - returns first fulfilled, ignores rejections until all fail
- Promise.resolve(value) - creates resolved Promise
- Promise.reject(error) - creates rejected Promise
Code Examples
Promise.all - Wait for All
const urls = ['/api/user', '/api/posts', '/api/comments']; const promises = urls.map(url => fetch(url)); Promise.all(promises) .then(responses => { // responses is an array in the same order as input console.log('All fetched successfully'); return Promise.all(responses.map(r => r.json())); }) .then(([user, posts, comments]) => { console.log(user, posts, comments); }) .catch(error => { // Fails fast - rejects immediately if any Promise rejects console.error('At least one failed:', error); });
Promise.all() waits for all Promises. Resolves with array of results. Rejects immediately if any fails.
Promise.race - First to Settle
const fetchWithTimeout = (url, timeout = 5000) => { return Promise.race([ fetch(url), new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeout) ) ]); }; fetchWithTimeout('/api/slow-endpoint') .then(response => console.log('Success')) .catch(error => console.log('Failed or timeout')); // Useful for: timeouts, responding to first available server
Promise.race() returns whichever Promise settles first, whether fulfilled or rejected.
Promise.allSettled - Never Fails
const promises = [ Promise.resolve('success'), Promise.reject('error'), Promise.resolve('another success') ]; Promise.allSettled(promises) .then(results => { // results is always an array of {status, value|reason} console.log(results); // [ // { status: 'fulfilled', value: 'success' }, // { status: 'rejected', reason: 'error' }, // { status: 'fulfilled', value: 'another success' } // ] const successes = results .filter(r => r.status === 'fulfilled') .map(r => r.value); console.log('Successes:', successes); }); // Never rejects, always resolves with status array
Promise.allSettled() waits for all Promises and never rejects. Returns status for each.
Promise.any - First Success
const urls = [ 'https://backup1.com/api', 'https://backup2.com/api', 'https://backup3.com/api' ]; const promises = urls.map(url => fetch(url)); Promise.any(promises) .then(response => { // First successful response console.log('First available server responded'); return response.json(); }) .catch(error => { // All failed - error is AggregateError console.log('All servers failed:', error.errors); }); // Ignores rejections until all fail
Promise.any() returns the first fulfilled Promise, ignoring rejections until all fail.
Common Mistakes
- Using Promise.all() when you need allSettled() behavior
- Not handling partial failures with Promise.all()
- Forgetting that Promise.all() rejects immediately on first failure
- Not using Promise.race() for timeout patterns
Interview Tips
- Know the difference between all/race/any/allSettled
- Implement Promise.all() from scratch
- Know when to use each method
- Understand fail-fast vs wait-for-all behavior