Skip to content
easy-cancelable-promise

Platform and versions

Where easy-cancelable-promise runs, what the package ships, and what changes when you upgrade from version 2.

GuideTypeScriptTry an example

Where it runs

The library is plain JavaScript on top of the native Promise. It has no dependencies and does not depend on any framework.

EnvironmentNeeds
BrowsersES2020: Chrome 80, Edge 80, Firefox 74, Safari 13.1
Node.js15 or later, for the global AbortController
Server renderingYes

CancelableAbortController extends the global AbortController. Everything else only needs Promise.

What the package ships

  • ES modules (.mjs), CommonJS (.cjs) and TypeScript declarations for every entry.
  • One entry per module, besides the main one. See TypeScript.
  • "sideEffects": false, so bundlers drop what you do not import.
  • No dependencies.

Bundlers and Node with import load the ES modules. require loads the CommonJS files.

Upgrading from version 2

The public API is the same. What changed is how the package is built, and a number of cases that did not behave as documented.

ChangeWhat to do
Published files are ES modules and CommonJSNothing with a bundler. The UMD global for <script> tags is gone
In Node, import X from 'easy-cancelable-promise' is the classBefore it was the object with all the exports. Use named imports
Every entry shares one classinstanceof now works between entries. Nothing to do
resolve(promise) keeps the status pendingThe status follows the promise you passed, and it can still be canceled
A callback that throws rejects the promiseBefore, the constructor threw. Move the try / catch to the promise
allSettled results are CancelablePromiseSettledResultA variable typed PromiseSettledResult needs the new type
groupAsCancelablePromise keeps the order of the tasksResults were in completion order. Stop sorting them
executeInOrder runs one task at a timeBefore, the tasks of a batch started together
Promises chained from a canceled promise are canceledTheir status was rejected. They still reject, so keep their catch

Release notes

  • cancel() now reaches a CancelablePromise used as the result of another promise.
  • A promise resolved with a pending promise stays pending, so it can be canceled; in 2.x that cancel was ignored.

New in version 3

  • beforeEachCallback and afterEachCallback receive the position of their task.
  • CancelablePromiseSettledResult, the type of the results of allSettled.

Fixed in version 3

  • A group with a task that is rejected now rejects. Before, it never settled.
  • Canceling a group no longer leaves unhandled rejections.
  • beforeEachCallback runs before its task starts.
  • maxConcurrent below 1 behaves as 1. Before, the group never finished.
  • CancelablePromise.all([]) and allSettled([]) resolve with an empty array.
  • all and allSettled keep one result per value when the same promise is passed twice.
  • resolve and reject after the promise settled are ignored, and the status no longer changes.
  • A cancel listener that throws no longer leaves the promise pending.
  • isCancelablePromise recognizes promises created by another copy of the package.
  • An error thrown by an async callback rejects the promise. Before, the promise stayed pending.
  • toCancelablePromise infers the type of its result. Before, then received a value typed as a promise.
  • The results of allSettled include the status 'canceled' in their type, CancelablePromiseSettledResult.
Documentation
↑↓ Navigate ↵ Open Esc Closeeasy-cancelable-promise docs
Get started

Promises you can actually control.