Demonstrates cancellation behavior across Promise combinators (all, any, race, allSettled) compared to native JavaScript Promise.
Dashboard bootstrap: four parallel widget requests (orders list, inventory check, stock price quote, deployment status). When one fails or settles early, what happens to the remaining pending requests?
In real applications, dashboard initialization fires multiple independent or interdependent requests in parallel. With native Promise, settling a combinator early (all on first reject, race on first settle, any on first fulfill) leaves losing requests running in the background, consuming bandwidth and server resources.
With CancelablePromise and @cancjs/coroutine:
canc.await.all: When one promise rejects, all remaining pending inputs are automatically canceled (loser-cancel).canc.await.any: On the first fulfillment, all remaining pending inputs are canceled.canc.await.race: On the first settlement (fulfillment or rejection), all remaining pending inputs are canceled.canc.await.allSettled: Waits for every input to settle; no loser-cancel by definition.bubble: falseisolation: Settingpromise.bubble = falseisolates an input from sibling cancellations so it survives when sibling requests fail.
src/all-vanilla.tsvssrc/all-canc.ts:Promise.allkeeps remaining requests running;CancelablePromise.allcancels them.src/any-vanilla.tsvssrc/any-canc.ts:Promise.anyleaves losers running;CancelablePromise.anycancels them.src/race-vanilla.tsvssrc/race-canc.ts:Promise.raceleaves losers running;CancelablePromise.racecancels them.src/all-settled-vanilla.tsvssrc/all-settled-canc.ts:allSettledwaits for all inputs; no cancellation in either flavor.src/isolation-vanilla.tsvssrc/isolation-canc.ts: uncancelablePromise.allin vanilla vsbubble: falseoption in canc.
Build the monorepo packages first:
npm run buildThen run the example entries from examples/:
# Vanilla (native Promise) behavior
npm run start:vanilla --prefix demo-combinators
# CancelablePromise behavior
npm run start:canc --prefix demo-combinatorsOr from within examples/demo-combinators/:
npm run start:vanilla
npm run start:canc
npm test- Vanilla: Loser requests continue running to completion (wasted network and compute).
- CancelablePromise: Loser requests receive cancel signals immediately and transition to aborted status.
- allSettled: All inputs settle independently with zero aborted calls.
- isolation: The
bubble: falsewidget completes while its failed siblings are canceled.
Cancellation occurs at the JavaScript promise and AbortSignal boundary. The simulated API endpoints in this example listen to the signal created by cancelify and abort their in-flight timers. In a production app, the underlying transport (e.g. fetch or http.request) must support AbortSignal to stop the in-flight network I/O.