Promise that can be canceled

Catch CancelError

import CancelablePromise from "@nextgis/cancelable-promise";

const promise = new CancelablePromise((resolve, reject) => {
setTimeout(() => resolve(), 100);
}).catch((er) => {
if (er.name === "CancelError") {
// handle cancel error
}
throw er;
});

promise.cancel();

Handle onCancel callback

import CancelablePromise from "@nextgis/cancelable-promise";

const promise = new CancelablePromise((resolve, reject, onCancel) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = () => {
resolve(xhr.responseText);
};
xhr.onerror = (er) => {
reject(er);
};

onCancel(() => {
xhr.abort();
});

xhr.send();
});

promise.cancel();

Type Parameters

  • T = any

Implements

  • Promise<T>

Constructors

  • Type Parameters

    • T = any

    Parameters

    • executor: ((resolve: ((value?: T | PromiseLike<T>) => void), reject: ((reason?: any) => void), onCancel: OnCancelFunction) => void)
        • (resolve, reject, onCancel): void
        • Parameters

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

                • Optionalvalue: T | PromiseLike<T>

                Returns void

          • reject: ((reason?: any) => void)
              • (reason?): void
              • Parameters

                • Optionalreason: any

                Returns void

          • onCancel: OnCancelFunction

          Returns void

    • Optionaltimeout: number

    Returns default<T>

Properties

[toStringTag]: string
id: number = ...
CancelError: typeof CancelError = CancelError
PromiseControl: typeof PromiseControl = PromiseControl
TimeoutError: typeof TimeoutError = TimeoutError

Methods

  • Attaches a callback for only the rejection of the Promise.

    Type Parameters

    • TResult = never

    Parameters

    • Optionalonrejected: null | ((reason: Error) => TResult | PromiseLike<TResult>)

      The callback to execute when the Promise is rejected.

    Returns default<T | TResult>

    A Promise for the completion of the callback.

  • Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

    Parameters

    • Optionalonfinally: null | (() => void)

      The callback to execute when the Promise is settled (fulfilled or rejected).

    Returns Promise<T>

    A Promise for the completion of the callback.

  • Attaches callbacks for the resolution and/or rejection of the Promise.

    Type Parameters

    • TResult1 = T
    • TResult2 = never

    Parameters

    • Optionalonfulfilled: null | ((value: T) => TResult1 | PromiseLike<TResult1>)

      The callback to execute when the Promise is resolved.

    • Optionalonrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

      The callback to execute when the Promise is rejected.

    Returns default<TResult1 | TResult2>

    A Promise for the completion of which ever callback is executed.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type Parameters

    • T

    Parameters

    • values: (T | PromiseLike<T>)[]

      An iterable of Promises.

    Returns default<T[]>

    A new Promise.

  • Parameters

    • Optionalopt: PromiseControlOptions

    Returns PromiseControl