Cancelling async work
AbortController and AbortSignal
Baseline widely available
Chrome66
Edge16
Firefox57
Safari12.1
Features it needs
- AbortController and AbortSignalWidely available
p-cancelable wraps a promise with a cancellation token you check manually. AbortController is a platform primitive doing the same job: fetch(), and increasingly other async APIs, accept a signal directly and stop the underlying work when it's aborted, not just stop you from acting on a result that already arrived.
When this applies
Cancelling an in-flight fetch or other signal-aware async operation.
The native approach
const controller = new AbortController();
fetch(url, { signal: controller.signal });
// later
controller.abort();When the dependency is still right
An answer that always says "the platform covers it" is worse than no answer. These are the cases where this one does not hold.
- You need to cancel a plain promise-returning function that was never written to check a signal. AbortSignal only cancels work that cooperates with it; wrapping an uncooperative promise still needs a library.
- You need to compose multiple abort reasons or chain a timeout, and your target browsers predate AbortSignal.any() and AbortSignal.timeout().