Type Parameters

  • ValueType

Hierarchy (view full)

Constructors

  • Create a promise that can be canceled.

    Can be constructed in the same was as a Promise constructor, but with an appended onCancel parameter in executor. PCancelable is a subclass of Promise.

    Cancelling will reject the promise with CancelError. To avoid that, set onCancel.shouldReject to false.

    Type Parameters

    • ValueType

    Parameters

    Returns default<ValueType>

    import PCancelable from 'p-cancelable';

    const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
    const job = new Job();

    onCancel.shouldReject = false;
    onCancel(() => {
    job.stop();
    });

    job.on('finish', resolve);
    });

    cancelablePromise.cancel(); // Doesn't throw an error

Properties

isCanceled: boolean

Whether the promise is canceled.

cancel: ((reason?: string) => void)

Cancel the promise and optionally provide a reason.

The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.

Type declaration

    • (reason?): void
    • Parameters

      • Optionalreason: string

        The cancellation reason to reject the promise with.

      Returns void

[toStringTag]: string

Methods

  • 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

    Returns Promise<Awaited<T>[]>

    A new Promise.

  • 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 extends [] | readonly unknown[]

    Parameters

    • values: T

      An array of Promises.

    Returns Promise<{
        -readonly [P in string | number | symbol]: Awaited<T[P<P>]>
    }>

    A new Promise.

  • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

    Type Parameters

    • T

    Parameters

    Returns Promise<Awaited<T>>

    A new Promise.

  • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An array of Promises.

    Returns Promise<Awaited<T[number]>>

    A new Promise.

  • Creates a new rejected promise for the provided reason.

    Type Parameters

    • T = never

    Parameters

    • Optionalreason: any

      The reason the promise was rejected.

    Returns Promise<T>

    A new rejected Promise.

  • Creates a new resolved promise.

    Returns Promise<void>

    A resolved promise.

  • Creates a new resolved promise for the provided value.

    Type Parameters

    • T

    Parameters

    • value: T

      A promise.

    Returns Promise<Awaited<T>>

    A promise whose internal state matches the provided promise.

  • Creates a new resolved promise for the provided value.

    Type Parameters

    • T

    Parameters

    Returns Promise<Awaited<T>>

    A promise whose internal state matches the provided promise.

  • 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<ValueType>

    A Promise for the completion of the callback.

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

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An array of Promises.

    Returns Promise<{
        -readonly [P in string | number | symbol]: PromiseSettledResult<Awaited<T[P<P>]>>
    }>

    A new Promise.

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

    Type Parameters

    • T

    Parameters

    Returns Promise<PromiseSettledResult<Awaited<T>>[]>

    A new Promise.

  • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An array or iterable of Promises.

    Returns Promise<Awaited<T[number]>>

    A new Promise.

  • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

    Type Parameters

    • T

    Parameters

    Returns Promise<Awaited<T>>

    A new Promise.

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

    Type Parameters

    • TResult = never

    Parameters

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

      The callback to execute when the Promise is rejected.

    Returns Promise<ValueType | TResult>

    A Promise for the completion of the callback.

  • Creates a new Promise and returns it in an object, along with its resolve and reject functions.

    Type Parameters

    • T

    Returns PromiseWithResolvers<T>

    An object with the properties promise, resolve, and reject.

    const { promise, resolve, reject } = Promise.withResolvers<T>();