Promise.allSettled() and Promise.race()
Promise.allSettled() and Promise.race() — the core idea
Promise.allSettled waits for every outcome and reports success or failure for each. Promise.race settles as soon as the first input settles, whether fulfilled or rejected. These methods answer different coordination questions.
What you’ll learn
- Explain promise.allsettled() and promise.race() 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: allSettled produces statuses fulfilled and rejected without throwing for the rejected member. In the race, the resolved fast promise wins; the second promise never settles, but does not keep Node alive.
Keep the boundary clear: Do not use Promise.race as cancellation. Losing promises still run unless an API provides explicit cancellation.
See the idea as a flow
Run the complete TypeScript example
Save the code as promise-allsettled-promise-race.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 outcomes = await Promise.allSettled([Promise.resolve("ok"), Promise.reject("missing")]);
console.log(outcomes.map(result => result.status).join(","));
const first = await Promise.race([Promise.resolve("fast"), new Promise<string>(() => {})]);
console.log(first);
}
main().catch(console.error);Verified output
fulfilled,rejected
fastBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable promise-allsettled-promise-race.ts; run: node promise-allsettled-promise-race.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
allSettled produces statuses fulfilled and rejected without throwing for the rejected member. In the race, the resolved fast promise wins; the second promise never settles, but does not keep Node alive.
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
Do not use Promise.race as cancellation. Losing promises still run unless an API provides explicit cancellation.
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
Inspect the status discriminator and print the value or reason for each allSettled 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.