Options
All
  • Public
  • Public/Protected
  • All
Menu

Cancellable promise. Extends the functionality of the native Promise to include the cancel method.

Example:


const cancellableFetchPromise = new CancellablePromise(async (resolve, reject, onCancel) => {
    const request = fetch("https://example.com/");

    onCancel(() => request.cancel());

    try {
        const response = await request;
        resolve(response);
    } catch (err) {
        reject(err);
    }
});

cancellableFetchPromise.cancel();

Type parameters

  • T

Hierarchy

  • Promise<T>
    • CancellablePromise

Index

Constructors

Methods

Constructors

constructor

  • new CancellablePromise<T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: string | Error) => void, onCancel: (cancellationFunction: () => void) => void) => void): CancellablePromise<T>
  • Creates a new CancellablePromise.

    Type parameters

    • T

    Parameters

    • executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: string | Error) => void, onCancel: (cancellationFunction: () => void) => void) => void

      A callback used to initialize the promise. This callback is passed three arguments: a resolve callback used to resolve the promise with a value or the result of another promise, a reject callback used to reject the promise with a provided reason or error, and an onCancel callback used to define behavior of cancellation.

        • (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: string | Error) => void, onCancel: (cancellationFunction: () => void) => void): void
        • Parameters

          • resolve: (value: T | PromiseLike<T>) => void
              • (value: T | PromiseLike<T>): void
              • Parameters

                • value: T | PromiseLike<T>

                Returns void

          • reject: (reason?: string | Error) => void
              • (reason?: string | Error): void
              • Parameters

                • Optional reason: string | Error

                Returns void

          • onCancel: (cancellationFunction: () => void) => void
              • (cancellationFunction: () => void): void
              • Parameters

                • cancellationFunction: () => void
                    • (): void
                    • Returns void

                Returns void

          Returns void

    Returns CancellablePromise<T>

Methods

cancel

  • Cancels the promise and invokes the cancellation callback if it was defined during instantiation. Cancellation will result in the promise being rejected.

    Returns CancellablePromise<T>