Curriculum
Concept 86 // Module 09: Asynchronous JavaScript & Browser APIs
Promise Combinators: all(), race(), any() & allSettled()
Module 09: Asynchronous JavaScript & Browser APIs•Concept 86 of 100• 5 min read
Promise Combinators: all(), race(), any() & allSettled()
JavaScript provides four Promise Combinators to coordinate multiple asynchronous operations in parallel: `Promise.all()`, `Promise.allSettled()`, `Promise.race()`, and `Promise.any()`.
The Core Concept
`Promise.any()` fulfills as soon as *any* promise fulfills. If all reject, it throws an `AggregateError` containing all rejection errors.
Syntax Reference
Promise.all([p1, p2]); // Fails fast if ANY reject
Promise.allSettled([p1, p2]); // Waits for ALL, never rejects
Promise.race([p1, p2]); // First to settle (resolve OR reject)
Promise.any([p1, p2]); // First to FULFILL (ignores rejections)Live Interactive Studio
Use `Promise.all` when all operations must succeed (all-or-nothing). Use `Promise.allSettled` when you need partial successes.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Promise.all vs Promise.allSettled
const p1 = Promise.resolve("Data 1");
const p2 = Promise.reject("Service Unavailable");
// Promise.all rejects immediately:
Promise.all([p1, p2])
.catch(err => console.log("Promise.all failed:", err));
// Promise.allSettled completes all promises safely:
Promise.allSettled([p1, p2])
.then(results => {
console.log(results);
// [
// { status: 'fulfilled', value: 'Data 1' },
// { status: 'rejected', reason: 'Service Unavailable' }
// ]
});Use `Promise.all` when all operations must succeed (all-or-nothing). Use `Promise.allSettled` when you need partial successes.
02
Request Timeout Pattern with Promise.race
const timeout = (ms) => new Promise((_, rej) =>
setTimeout(() => rej(new Error("Timeout")), ms)
);
function fetchWithTimeout(url, ms) {
return Promise.race([
fetch(url),
timeout(ms)
]);
}`Promise.race()` settles with whichever promise completes first, making it ideal for timeout guards.
03
1. Core Usage: Promise Combinators
// Core syntax and usage for Promise Combinators: all(), race(), any() & allSettled()
const feature = 'promise-combinators-all-race-any-allsettled';
console.log('Concept 86:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Promise Combinators: all(), race(), any() & allSettled().
04
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('promise-combinators-all-race-any-allsettled'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Promise Combinators: all(), race(), any() & allSettled().
05
3. Clean Architecture & Production Pattern
// Production-grade pattern for Promise Combinators: all(), race(), any() & allSettled()
const settings = Object.freeze({
id: 'promise-combinators-all-race-any-allsettled',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Batch Data Loading: Loading user profile, notifications, and feed data simultaneously with `Promise.all`.
Dashboard Metrics: Loading widgets independently with `Promise.allSettled` so one failure doesn't break the page.
Fastest Mirror Fetching: Querying multiple CDN edge mirrors with `Promise.any`.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
Combinators allocate an internal counter and result array. Each incoming promise attaches reaction handlers that decrement the remaining counter until completion conditions are met.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Promise Combinators: all(), race(), any() & allSettled() in JavaScript?
// Concept 86: Promise Combinators: all(), race(), any() & allSettled()
// Understanding the core mental model1 / 10