Sub topic 5
Fail-Fast: The First Rejection Wins, the Rest Keep Running
This chapter assumes you already know how to create promises, use async/await with try/catch, and run independent tasks together with Promise.all. Here we look at what happens when one of those tasks fails.
The Rule
Promise.all rejects as soon as any input rejects. It does not wait for the other inputs to settle. The rejection reason is the reason of the first input to reject in time order, which is not necessarily the first input in the array.
That is the "fail-fast" behavior. The other part matters just as much: the other operations are not stopped. A promise is only a handle on a result. Rejecting the combined promise has no way to halt a timer, a network request, or a database write that is already in progress.
Seeing It Happen
const task = (name, ms, fail = false) =>
new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`${name} finished after ${ms} ms`);
if (fail) reject(new Error(`${name} failed`));
else resolve(name);
}, ms);
});
async function main() {
const start = Date.now();
try {
await Promise.all([
task('slow', 300),
task('broken', 100, true),
task('medium', 200)
]);
} catch (err) {
console.log(`caught "${err.message}" at ~${Date.now() - start} ms`);
}
}
main();
// broken finished after 100 ms
// caught "broken failed" at ~100 ms
// medium finished after 200 ms
// slow finished after 300 msThe catch block runs at about 100 ms. The medium and slow tasks still finish afterwards, and their results go nowhere.
Consequences to Plan For
| Situation | What happens |
|---|---|
| A second input rejects later | Its reason is discarded. `Promise.all` already attached a handler, so you get no unhandled-rejection warning. |
| Remaining tasks have side effects | They still happen: emails get sent and rows get written, even though your code took the error path. |
| You retry right after the failure | The old tasks may still be running alongside the retry. |
| You need every error | Fail-fast only gives you one. Use `Promise.allSettled`, which a later chapter covers. |
Why Not Just Await Each Promise Separately?
Some code starts promises early and then awaits them one at a time to get concurrency without Promise.all. That pattern has a hidden flaw:
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const fail = (ms) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(`failed after ${ms} ms`)), ms));
async function risky() {
const a = wait(200);
const b = fail(50);
await a; // b rejects while we are paused here
await b;
}
risky().catch((err) => console.log('caught:', err.message));b rejects at 50 ms, but nothing is handling it yet, because the function is still paused on a. Node.js reports an unhandled rejection and, by default, exits the process before the catch ever runs. Writing await Promise.all([a, b]) fixes this. Promise.all subscribes to every input right away, so any rejection is handled, and the first one also arrives sooner.
Exercise
Promise.all is given three tasks: A fulfills at 300 ms, B rejects with "B" at 250 ms, and C rejects with "C" at 100 ms. When does the returned promise reject, with which reason, and does A's timer still fire?
Show answer
It rejects at about 100 ms with reason "C", because C is the first input to reject in time order. B's later rejection is ignored without an unhandled-rejection warning. A's timer still fires at 300 ms, because Promise.all does not cancel the remaining work.
Exercise
In the risky() example, why does the rejection go unhandled, and what one-line change fixes it?
Show answer
Promise b rejects at 50 ms while the function is paused on await a, so no handler is attached to b yet. Replacing the two awaits with await Promise.all([a, b]); attaches handlers to both promises at once.