Class PCancelable<ValueType>

Type Parameters

  • ValueType

Hierarchy

  • Promise<ValueType>
    • PCancelable

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

    • executor: ((resolve, reject, onCancel) => void)
        • (resolve, reject, onCancel): void
        • Parameters

          • resolve: ((value?) => void)
              • (value?): void
              • Parameters

                • Optional value: ValueType | PromiseLike<ValueType>

                Returns void

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

                • Optional reason: unknown

                Returns void

          • onCancel: OnCancelFunction

          Returns void

    Returns PCancelable<ValueType>

    Example

    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

[toStringTag]: string
cancel: ((reason?) => void)

Type declaration

    • (reason?): 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.

      Parameters

      • Optional reason: string

        The cancellation reason to reject the promise with.

      Returns void

isCanceled: boolean

Whether the promise is canceled.

[species]: PromiseConstructor

Methods

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

    Type Parameters

    • TResult = never

    Parameters

    • Optional onrejected: null | ((reason) => TResult | PromiseLike<TResult>)

      The callback to execute when the Promise is rejected.

    Returns Promise<ValueType | 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

    • Optional onfinally: 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.

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

    Type Parameters

    • TResult1 = ValueType

    • TResult2 = never

    Parameters

    • Optional onfulfilled: null | ((value) => TResult1 | PromiseLike<TResult1>)

      The callback to execute when the Promise is resolved.

    • Optional onrejected: null | ((reason) => TResult2 | PromiseLike<TResult2>)

      The callback to execute when the Promise is rejected.

    Returns Promise<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: Iterable<T | PromiseLike<T>>

      An iterable of Promises.

    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]>
    }>

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

    Parameters

    • values: T

      An array of Promises.

    Returns Promise<{
        -readonly [P in string | number | symbol]: PromiseSettledResult<Awaited<T[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

    • values: Iterable<T | PromiseLike<T>>

      An array of Promises.

    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

    • values: Iterable<T | PromiseLike<T>>

      An array or iterable of Promises.

    Returns Promise<Awaited<T>>

    A new Promise.

  • Convenience method to make your promise-returning or async function cancelable.

    Type Parameters

    • ReturnType

    Parameters

    • userFn: ((onCancel) => PromiseLike<ReturnType>)
        • (onCancel): PromiseLike<ReturnType>
        • Parameters

          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns (() => PCancelable<ReturnType>)

      • (): PCancelable<ReturnType>
      • Convenience method to make your promise-returning or async function cancelable.

        Returns PCancelable<ReturnType>

        Example

        import PCancelable from 'p-cancelable';

        const fn = PCancelable.fn((input, onCancel) => {
        const job = new Job();

        onCancel(() => {
        job.cleanup();
        });

        return job.start(); //=> Promise
        });

        const cancelablePromise = fn('input'); //=> PCancelable

        // …

        cancelablePromise.cancel();

    Example

    import PCancelable from 'p-cancelable';

    const fn = PCancelable.fn((input, onCancel) => {
    const job = new Job();

    onCancel(() => {
    job.cleanup();
    });

    return job.start(); //=> Promise
    });

    const cancelablePromise = fn('input'); //=> PCancelable

    // …

    cancelablePromise.cancel();
  • Type Parameters

    • Agument1Type

    • ReturnType

    Parameters

    • userFn: ((argument1, onCancel) => PromiseLike<ReturnType>)
        • (argument1, onCancel): PromiseLike<ReturnType>
        • Parameters

          • argument1: Agument1Type
          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns ((argument1) => PCancelable<ReturnType>)

  • Type Parameters

    • Agument1Type

    • Agument2Type

    • ReturnType

    Parameters

    • userFn: ((argument1, argument2, onCancel) => PromiseLike<ReturnType>)
        • (argument1, argument2, onCancel): PromiseLike<ReturnType>
        • Parameters

          • argument1: Agument1Type
          • argument2: Agument2Type
          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns ((argument1, argument2) => PCancelable<ReturnType>)

      • (argument1, argument2): PCancelable<ReturnType>
      • Parameters

        • argument1: Agument1Type
        • argument2: Agument2Type

        Returns PCancelable<ReturnType>

  • Type Parameters

    • Agument1Type

    • Agument2Type

    • Agument3Type

    • ReturnType

    Parameters

    • userFn: ((argument1, argument2, argument3, onCancel) => PromiseLike<ReturnType>)
        • (argument1, argument2, argument3, onCancel): PromiseLike<ReturnType>
        • Parameters

          • argument1: Agument1Type
          • argument2: Agument2Type
          • argument3: Agument3Type
          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns ((argument1, argument2, argument3) => PCancelable<ReturnType>)

      • (argument1, argument2, argument3): PCancelable<ReturnType>
      • Parameters

        • argument1: Agument1Type
        • argument2: Agument2Type
        • argument3: Agument3Type

        Returns PCancelable<ReturnType>

  • Type Parameters

    • Agument1Type

    • Agument2Type

    • Agument3Type

    • Agument4Type

    • ReturnType

    Parameters

    • userFn: ((argument1, argument2, argument3, argument4, onCancel) => PromiseLike<ReturnType>)
        • (argument1, argument2, argument3, argument4, onCancel): PromiseLike<ReturnType>
        • Parameters

          • argument1: Agument1Type
          • argument2: Agument2Type
          • argument3: Agument3Type
          • argument4: Agument4Type
          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns ((argument1, argument2, argument3, argument4) => PCancelable<ReturnType>)

      • (argument1, argument2, argument3, argument4): PCancelable<ReturnType>
      • Parameters

        • argument1: Agument1Type
        • argument2: Agument2Type
        • argument3: Agument3Type
        • argument4: Agument4Type

        Returns PCancelable<ReturnType>

  • Type Parameters

    • Agument1Type

    • Agument2Type

    • Agument3Type

    • Agument4Type

    • Agument5Type

    • ReturnType

    Parameters

    • userFn: ((argument1, argument2, argument3, argument4, argument5, onCancel) => PromiseLike<ReturnType>)
        • (argument1, argument2, argument3, argument4, argument5, onCancel): PromiseLike<ReturnType>
        • Parameters

          • argument1: Agument1Type
          • argument2: Agument2Type
          • argument3: Agument3Type
          • argument4: Agument4Type
          • argument5: Agument5Type
          • onCancel: OnCancelFunction

          Returns PromiseLike<ReturnType>

    Returns ((argument1, argument2, argument3, argument4, argument5) => PCancelable<ReturnType>)

      • (argument1, argument2, argument3, argument4, argument5): PCancelable<ReturnType>
      • Parameters

        • argument1: Agument1Type
        • argument2: Agument2Type
        • argument3: Agument3Type
        • argument4: Agument4Type
        • argument5: Agument5Type

        Returns PCancelable<ReturnType>

  • Type Parameters

    • ReturnType

    Parameters

    • userFn: ((...arguments) => PromiseLike<ReturnType>)
        • (...arguments): PromiseLike<ReturnType>
        • Parameters

          • Rest ...arguments: unknown[]

          Returns PromiseLike<ReturnType>

    Returns ((...arguments) => PCancelable<ReturnType>)

      • (...arguments): PCancelable<ReturnType>
      • Parameters

        • Rest ...arguments: unknown[]

        Returns PCancelable<ReturnType>

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

    Type Parameters

    • T

    Parameters

    • values: Iterable<T | PromiseLike<T>>

      An iterable of Promises.

    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

    • Optional reason: 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

    • value: T | PromiseLike<T>

      A promise.

    Returns Promise<Awaited<T>>

    A promise whose internal state matches the provided promise.