Small API.
Clear mental model.
Start with one promise you can cancel. Add progress, groups and deferred promises as your application grows.
const task = new CancelablePromise((resolve, reject, { onCancel }) => {
onCancel(() => stopTheWork());
});
task.cancel();Whoever holds the promise cancels it.
The promise stops its own work.
A guide for every layer.
Core concepts
CancelablePromise
The class behind everything else. How it is created, what its callback receives, how its status changes and what happens in a chain.
cancel and onCancel
Cancel a promise from anywhere, stop its work with onCancel, pass a reason, remove listeners and tell a cancellation from a failure.
Progress reporting
Report how far the work is from inside a promise, listen from outside, follow it through a chain and stop listening.
defer and DeferredPromise
Create a CancelablePromise whose resolve, reject and utilities live outside its callback, for events, queues and tests.
Static helpers
CancelablePromise.all, allSettled, race, resolve, reject and canceled. The static methods of Promise, returning promises that can be canceled.
Guides
Converting native promises
Turn a Promise, a value or a function into a CancelablePromise with toCancelablePromise, know what a wrapper can and cannot stop, and check what you have with the type guards.
Groups and concurrency
Run a list of tasks with groupAsCancelablePromise, limit how many run at once, follow their progress and cancel all of them with one call.
Abort signals
Remove cancel and progress listeners with an AbortSignal, and manage many subscriptions with CancelableAbortController.
TypeScript
The type arguments of each function, the exported types, how to narrow rejection reasons, and the entries of the package.
Quality
See it run before you read.
Three live examples, each with its tested source.