Promise.all
Wait for all
Promise dot all takes a list of promises and fulfills with a list of values in the same order. All the inputs run at the same time. Await the single combined promise to get every result together. The program awaits both promises and prints the list they fulfil with.
async function loadTwo(): Promise<void> { const vals: number[] = await Promise.all([ Promise.resolve(1), Promise.resolve(2), ]); console.log(vals); } loadTwo();
[ 1, 2 ]
One rejects
If any input rejects, the whole all call rejects right away. Other inputs keep running in the background, but their values are dropped. Wrap the await in try and catch when any failure must be handled. The program prints the message of that one rejection.
async function demoFail(): Promise<void> { try { await Promise.all([ Promise.resolve(1), Promise.reject(new Error("bad")), ]); } catch (err: unknown) { console.log((err as Error).message); } } demoFail();
bad
Settled report
When every outcome matters, use allSettled instead of all. It never rejects for input failures. The result list tells for each slot whether it fulfilled or rejected. The program settles one of each and prints how many reports came back.
async function report(): Promise<void> { const out = await Promise.allSettled([ Promise.resolve(1), Promise.reject(new Error("bad")), ]); console.log(out.length); } report();
2
Map then all
A common pattern maps a list of inputs to a list of promises, then awaits all at once. Map starts every task, all collects the answers. The values keep the same order as the inputs. The program prints the doubled ids in the order of the list.
async function double(n: number): Promise<number> { return n * 2; } async function main(): Promise<void> { const ids: number[] = [1, 2, 3]; const vals: number[] = await Promise.all(ids.map(double)); console.log(vals); } main();
[ 2, 4, 6 ]
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
Promise.allfulfill with?Answer
One list of the values, in the order of the input promises
All waits for every input and keeps the order of the list it was given, whatever order the promises settle in.
-
What happens when one input of
Promise.allrejects?Answer
The whole call rejects at once with that reason
The first rejection settles the combined promise, and the other inputs keep running with their results dropped.
-
When is
Promise.allSettledthe better choice thanPromise.all?Answer
When every outcome must be reported and a failed input must not throw
AllSettled never rejects for an input failure, so the program gets a report for every slot.
-
What does each entry of a
Promise.allSettledresult hold?Answer
An object with
statusand eithervalueorreasonA fulfilled slot has
status: "fulfilled"and a value, a rejected one hasstatus: "rejected"and a reason. -
What does this program print?
async function loadTwo(): Promise<void> { const vals: number[] = await Promise.all([ Promise.resolve(1), Promise.resolve(2), ]); console.log(vals); } loadTwo();Answer
[ 1, 2 ]
Both inputs fulfil with a number, and the combined promise fulfils with the list of both values.
-
What does this program print?
async function demoFail(): Promise<void> { try { await Promise.all([ Promise.resolve(1), Promise.reject(new Error("bad")), ]); } catch (err: unknown) { console.log((err as Error).message); } } demoFail();Answer
bad
The rejected input settles the call at once, the catch takes the error and the program prints its message.
-
What does this program print?
async function report(): Promise<void> { const out = await Promise.allSettled([ Promise.resolve(1), Promise.reject(new Error("bad")), ]); console.log(out.length); } report();Answer
2
One input fulfils and one rejects, and both outcomes get a report, so the list holds two entries.
-
What does this program print?
async function double(n: number): Promise<number> { return n * 2; } async function main(): Promise<void> { const ids: number[] = [1, 2, 3]; const vals: number[] = await Promise.all(ids.map(double)); console.log(vals); } main();Answer
[ 2, 4, 6 ]
Map turns every id into a promise and all collects the doubled values in the order of the ids.
-
A program needs every result even when one request fails. Why is
Promise.allthe wrong tool there?Answer
The first rejection throws and the values of the other inputs are dropped
All rejects with the first failure and loses the other results, while
Promise.allSettledreports every outcome. -
What does the compiler say about this program?
async function main(): Promise<void> { const vals: string[] = await Promise.all([Promise.resolve(1)]); console.log(vals); } main();Answer
error TS2322: Type 'number[]' is not assignable to type 'string[]'.
All keeps the element type of its inputs, so a list of promises of numbers gives
number[], notstring[].
Article author: Arthur Isaev