Promise.all()
Promise.all() — the core idea
Promise.all waits until every input fulfills and preserves the input order of results, not completion order. If any input rejects, the combined Promise rejects. Other already-started operations are not automatically cancelled.
What you’ll learn
- Explain promise.all() with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The two promises are already created, then Promise.all combines their fulfilled numbers into [3,5]. Array order matches the input positions. The sum is 8.
Keep the boundary clear: Promise.all is not an automatic cancellation manager; handle aborts explicitly if a failed operation should stop siblings.
See the idea as a flow
Run the complete TypeScript example
Save the code as promise-all.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
async function main(): Promise<void> {
const results = await Promise.all([Promise.resolve(3), Promise.resolve(5)]);
console.log(results.join(","));
console.log(results.reduce((a, b) => a + b, 0));
}
main().catch(console.error);Verified output
3,5
8Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable promise-all.ts; run: node promise-all.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The two promises are already created, then Promise.all combines their fulfilled numbers into [3,5]. Array order matches the input positions. The sum is 8.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Promise.all is not an automatic cancellation manager; handle aborts explicitly if a failed operation should stop siblings.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Combine one Promise<string> and one Promise<number> and inspect the inferred tuple result.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
Promise.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.