Sub topic 3
What a Promise Combinator Does
This chapter assumes you can create promises, use then and await, and know that a promise is pending, fulfilled, or rejected. Before looking at each combinator one at a time, it helps to see the model that all four share. Once you know that model, each combinator comes down to one rule about when it settles.
Iterable In, One Promise Out
A promise combinator is a static method on Promise that takes an iterable of values and returns a single new promise. JavaScript has four of them: Promise.all, Promise.allSettled, Promise.race, and Promise.any.
Every combinator follows the same steps:
- It reads the iterable. This can be an array, a
Set, a generator, or anything else that works withfor...of. - It passes each item through
Promise.resolve, so plain values become promises that are already fulfilled. - It attaches handlers to each of those promises.
- It returns its own promise straight away, in the pending state.
- Later, when its rule is met, it settles that returned promise once.
Combinators Watch Work; They Don't Start It
A combinator never starts any work. By the time you call Promise.all(...), the timers, requests, or file reads behind the input promises are already running. The combinator only watches them settle. This explains two behaviors you will meet later. A combinator cannot make tasks run in parallel if you awaited them one by one before passing them in. It also cannot stop tasks that are still running after it has settled.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const inputs = new Set([delay(50, 'a'), 'b']); // any iterable works
const combined = Promise.all(inputs);
console.log(combined instanceof Promise); // true
console.log('combinator returned immediately');
combined.then((values) => console.log(values)); // ['a', 'b'] after ~50 msThe log lines print before the result. The combinator handed back a pending promise at once, and the plain string 'b' was treated as a promise that had already fulfilled.
Four Rules for Settling
The combinators differ only in what they wait for and what they produce.
| Combinator | Fulfills when | Rejects when | Fulfillment value |
|---|---|---|---|
| `Promise.all` | every input fulfills | any input rejects (first one) | array of values in input order |
| `Promise.allSettled` | every input settles | never, in normal use | array of `{ status, value }` or `{ status, reason }` objects |
| `Promise.race` | the first input to settle fulfills | the first input to settle rejects | that input's value |
| `Promise.any` | any input fulfills (first one) | every input rejects | that input's value |
Two of these rules short-circuit. Promise.all stops waiting at the first rejection, and Promise.any stops at the first fulfillment. Promise.race decides on the first settlement of either kind. Only Promise.allSettled always waits for every input.
Common Mistakes
- Passing functions instead of promises.
Promise.all([fetchA, fetchB])fulfills with the two functions themselves, because functions are plain values. Call them:Promise.all([fetchA(), fetchB()]). - Expecting the combinator to cancel losers. When
raceoranysettles, the other operations keep running. - Passing a non-iterable.
Promise.all(somePromise)returns a rejected promise with aTypeError. It does not throw synchronously.
Exercise
What does this log, and roughly when? Promise.race([new Promise((r) => setTimeout(() => r('slow'), 100)), 'fast']).then(console.log);
Show answer
It logs 'fast' almost immediately. The plain string goes through Promise.resolve and becomes a promise that has already fulfilled, so it settles before the 100 ms timer fires and decides the race.
Exercise
A teammate writes const results = await Promise.all([await getUser(), await getOrders()]); and expects the two calls to run concurrently. Why don't they?
Show answer
Each inner await finishes before the array is even built, so getOrders() starts only after getUser() has finished. By the time Promise.all receives the values, the work is already done. Remove the inner awaits so that both promises start first and Promise.all waits for them together.