Typorithm · medium

Promise Combinators

promise-combinatorsinterview-questionspromise-allpromise-racepromise-any
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Promise Combinators

0%

Async & Event Loop

Promise.all rejects on the first rejection but otherwise resolves to an array; Promise.race settles with whichever promise settles first (fulfill or reject); Promise.allSettled waits for all and never rejects; Promise.any takes the first fulfillment and rejects only if all reject, with an AggregateError.

Promise.all([...]) resolves to an array of results once every promise fulfills, but it short-circuits and rejects with the reason of the first promise to reject — so one failure fails the whole batch.

Promise.race([...]) settles with the first promise to settle, fulfilling or rejecting to match it; Promise.any([...]) instead waits for the first fulfillment and only rejects, with an AggregateError, if every promise rejects.

Promise.allSettled([...]) is the forgiving one: it waits for all promises and never rejects, resolving to an array of {status: "fulfilled", value} or {status: "rejected", reason} objects so you can inspect each outcome.

01

all → array on all-fulfill, rejects on first rejection.

02

race → first to settle (fulfill OR reject); any → first fulfill.

03

allSettled → waits for all, never rejects; any rejects only if all reject (AggregateError).

Promise.all([a, b])        // all fulfill → array; first reject → rejects
Promise.race([a, b])       // first to settle (fulfill OR reject) wins
Promise.allSettled([a, b]) // waits for all; never rejects
Promise.any([a, b])        // first fulfill; all reject → AggregateError

Read the implementation

Connect each part of the code to the concept before you type.

1. Promise.all

Promise.all resolves to an array of values when every input fulfills, but rejects as soon as any one rejects.

Promise.all([a, b])        // all fulfill → array; first reject → rejects

2. Promise.race

Promise.race settles with the first promise to settle, whether it fulfills or rejects.

Promise.race([a, b])       // first to settle (fulfill OR reject) wins

3. Promise.allSettled

Promise.allSettled waits for every promise and never rejects, returning a {status, value|reason} per input.

Promise.allSettled([a, b]) // waits for all; never rejects

4. Promise.any

Promise.any fulfills with the first fulfillment and only rejects (with an AggregateError) if every input rejects.

Promise.any([a, b])        // first fulfill; all reject → AggregateError

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Promise Combinators: Lesson & Practice · Typorithm | ApplyDjinn