Skip to content
easy-cancelable-promise

API reference

Every export of easy-cancelable-promise in one place. CancelablePromise, defer, groupAsCancelablePromise, toCancelablePromise, the type guards, CancelableAbortController and the types.

API referenceTypeScriptTry an example

Exports

ts
import {
  CancelablePromise,
  defer,
  groupAsCancelablePromise,
  toCancelablePromise,
  isCancelablePromise,
  isPromise,
  CancelableAbortController,
  isCancelableAbortSignal,
} from 'easy-cancelable-promise';

CancelablePromise is also the default export. Every module is its own entry too, for example easy-cancelable-promise/defer. See TypeScript.

CancelablePromise

ts
import { CancelablePromise } from 'easy-cancelable-promise';

A class that extends Promise<T>.

Constructor

new CancelablePromise<T>(callback)

The callback receives resolve, reject and the utilities.

ParameterTypePurpose
resolveResolveCallback<T>Resolves the promise with a value or with another promise
rejectRejectCallbackRejects the promise
utilsCancelablePromiseUtils<T>The utilities below
UtilityReturnsPurpose
utils.cancel(reason?)The promiseCancels the promise from inside
utils.onCancel(callback)SubscriptionListens to the cancellation
utils.onProgress(callback)SubscriptionListens to the progress
utils.reportProgress(value, metadata?)voidSends progress to the listeners
utils.status()PromiseStatusThe current status
utils.isPending()booleantrue while the promise is not settled
utils.isCanceled()booleantrue after the promise was canceled

An error thrown by the callback rejects the promise, also when the callback is async.

Instance members

MemberReturnsPurpose
statusPromiseStatus'pending', 'resolved', 'rejected' or 'canceled'
cancel(reason?)The promiseCancels a pending promise. Default reason: new Error('Promise canceled')
onCancel(callback, { signal }?)The promiseListens to the cancellation
onProgress(callback, { signal }?)The promiseListens to the progress
reportProgress(value, metadata?)The promiseSends progress to the listeners
then(onFulfilled?, onRejected?)CancelablePromiseSame as Promise. Canceling the result cancels this promise
catch(onRejected?)CancelablePromiseSame as Promise
finally(onFinally?)CancelablePromiseSame as Promise

cancel calls the listeners registered through the utilities first, then the listeners registered on the promise, and then rejects the promise with the reason. If a listener throws, cancel throws that error after the promise is canceled.

Static methods

MethodReturns
CancelablePromise.all(values)A promise of every result, in the order of the values
CancelablePromise.allSettled(values)A promise of a CancelablePromiseSettledResult for every value
CancelablePromise.race(values)A promise that settles with the first value to settle
CancelablePromise.resolve(value?)A resolved promise
CancelablePromise.reject(reason?)A rejected promise
CancelablePromise.canceled(reason?)A canceled promise

Canceling the result of all, allSettled or race cancels every promise inside it. In all and race, a promise that is canceled cancels the result.

defer

ts
import { defer } from 'easy-cancelable-promise';

defer<T>() returns a DeferredPromise<T>.

PropertyType
promiseCancelablePromise<T>
resolveResolveCallback<T>
rejectRejectCallback
The utilitiesCancelablePromiseUtils<T>

See defer and DeferredPromise.

groupAsCancelablePromise

ts
import { groupAsCancelablePromise } from 'easy-cancelable-promise';

groupAsCancelablePromise<T>(sources, config?) returns a CancelablePromise<T>, or null when sources is empty.

sources is an array. Each item is a function that returns a promise, a CancelablePromise or a Promise.

OptionTypeDefaultPurpose
maxConcurrentnumber8How many sources run at the same time
executeInOrderbooleanfalseStarts each source after the previous one finished
beforeEachCallback(index: number) => voidnoneCalled before each source starts
afterEachCallback(result: unknown, index: number) => voidnoneCalled after each source that succeeded
onQueueEmptyCallback(result: unknown[] | null) => voidnoneCalled once, when every source succeeded

The promise resolves with the results in the order of the sources, reports the percentage of sources completed, rejects when a source is rejected, and cancels the pending sources when it is canceled. See Groups and concurrency.

toCancelablePromise

ts
import { toCancelablePromise } from 'easy-cancelable-promise';

toCancelablePromise(source) returns a CancelablePromise of the result of the source. The type is inferred.

sourceResult
A CancelablePromiseThe same instance
A PromiseA cancelable promise that follows it
A functionThe function is called and its result is converted
Any other valueA cancelable promise resolved with the value

See Converting native promises.

isCancelablePromise

ts
import { isCancelablePromise } from 'easy-cancelable-promise';

isCancelablePromise<T>(value) returns true when the value is a CancelablePromise, and narrows it to CancelablePromise<T>. It recognizes promises created by another copy of the package.

isPromise

ts
import { isPromise } from 'easy-cancelable-promise';

isPromise(value) returns true when the value has a then method, and narrows it to Promise<unknown>.

CancelableAbortController

ts
import { CancelableAbortController } from 'easy-cancelable-promise';

A class that extends AbortController.

MemberReturnsPurpose
signalCancelableAbortSignalThe signal, with subscribe
signal.subscribe(listener, options?)RemoveEventListenerListens to the abort event
signal.subscribe(type, listener, options?)RemoveEventListenerListens to another event type
subscriptionsRemoveEventListener[]The active subscriptions
abort()voidAborts the signal and removes every subscription
dispose()voidRemoves every subscription without aborting

subscribe throws after abort or dispose. See Abort signals.

isCancelableAbortSignal

ts
import { isCancelableAbortSignal } from 'easy-cancelable-promise';

isCancelableAbortSignal(signal) returns true for the signal of a CancelableAbortController, and narrows it to CancelableAbortSignal.

Types

ts
import type {
  PromiseStatus,
  CancelablePromiseCallback,
  CancelablePromiseUtils,
  ResolveCallback,
  RejectCallback,
  CancelCallback,
  OnProgressCallback,
  Subscription,
  SubscriptionParams,
  DeferredPromise,
  CancelablePromiseSettledResult,
  CancelablePromiseGroupConfig,
  CancelablePromiseBuildCallback,
  CancelableAbortSignal,
  RemoveEventListener,
} from 'easy-cancelable-promise';
TypeDefinition
PromiseStatus'canceled' | 'pending' | 'resolved' | 'rejected'
CancelablePromiseCallback<T>(resolve, reject, utils) => void
CancelablePromiseUtils<T>The utilities of the callback
ResolveCallback<T>(value?: T | PromiseLike<T>) => void
RejectCallback(reason?: unknown) => void
CancelCallback(reason?: unknown) => void
OnProgressCallback(progress: number, metadata?: unknown) => void
Subscription() => void
SubscriptionParams{ signal?: CancelableAbortSignal | AbortSignal }
DeferredPromise<T>{ promise, resolve, reject } and the utilities
CancelablePromiseSettledResult<T>PromiseSettledResult<T> | { status: 'canceled'; reason: unknown }
CancelablePromiseGroupConfigThe options of groupAsCancelablePromise
CancelablePromiseBuildCallback<T>() => Promise<T> | CancelablePromise<T>
CancelableAbortSignalAbortSignal with subscribe
RemoveEventListener() => void
Documentation
↑↓ Navigate ↵ Open Esc Closeeasy-cancelable-promise docs
Get started

Promises you can actually control.