Cancellation and error propagation
Local practice: Node.js 22.16.0 on Linux x64; proposed teaching baseline 26.13.1 remains untested. Copy only the listed files into a fresh disposable directory. No dependency installation or external services required. These are static Node.js examples, not verified ByteMind runner tasks.
Inspect success and failure independently
const tasks = [Promise.resolve('stock'), Promise.reject(new Error('missing'))];const outcomes = await Promise.allSettled(tasks);console.log(outcomes.map(x => x.status).join(','));console.log(await Promise.any(tasks));try { await Promise.all(tasks); } catch (err) { console.log(err.message); }Run:
node main.mjsExpected stdout:
fulfilled,rejectedstockmissingExit status: 0. Expected stderr: empty.
How it works
Use try/catch for expected failures and finally for cleanup. Promise.all rejects when one input rejects; it does not stop sibling operations. allSettled reports every outcome, race settles with the first settled input, and any fulfills with the first successful input or rejects with AggregateError when none succeeds. Start only a bounded number of operations per group; promises do not impose capacity limits. Pass an AbortController signal to APIs that accept it; aborting a signal cannot undo a completed side effect.
Cancel a supported timer
import { setTimeout as delay } from 'node:timers/promises';const controller = new AbortController();const pending = delay(1000, 'done', { signal: controller.signal });controller.abort();try { console.log(await pending); }catch (err) { if (err.name !== 'AbortError') throw err; console.log(err.name); }finally { console.log('cleanup'); }Run:
node main.mjsExpected stdout:
AbortErrorcleanupExit status: 0. Expected stderr: empty.
Common mistake
Do not treat Promise.race against a timer as cancellation of the losing task. A timeout policy also needs cancellation support, cleanup and clear ownership. Avoid swallowing unexpected errors.
Practice
Use allSettled on one success and two failures. Explain what changes with any. In the timer example, remove the abort and predict the result (one second delay).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cancellation and error propagation — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What happens to siblings after Promise.all rejects?
Which combinator preserves each success or failure?
What must an operation do for an AbortSignal to be effective?