lifecycle-utils
    Preparing search index...

    Class AbortablePromise<T>

    A Promise that can be rejected by an abort signal.

    When a signal is provided, the promise is rejected with signal.reason if the signal is aborted before the executor resolves or rejects the promise.

    The executor may return a cleanup callback, which is invoked when the executor calls resolve or reject, or when the signal aborts it.

    The resolved argument passed to the cleanup callback is true when the executor called resolve, and false otherwise.

    import {AbortablePromise} from "lifecycle-utils";

    const controller = new AbortController();

    const promise = new AbortablePromise(controller.signal, (resolve, reject) => {
    // register to things here

    return (resolved) => {
    // do cleanup here

    // `resolved` is true if `resolve` was called,
    // and false if `reject` was called or the signal was aborted
    };
    });

    Type Parameters

    • T

    Hierarchy

    Index

    Constructors

    Methods

    • Like PromiseConstructor.all, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • signal: AbortSignal | undefined
      • values: T

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

    • 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

      • signal: AbortSignal | undefined
      • values: Iterable<T | PromiseLike<T>>

        An array of Promises.

      Returns Promise<Awaited<T>[]>

      A new Promise.

    • Like PromiseConstructor.all, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

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

    • Like PromiseConstructor.all, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T

      Parameters

      Returns Promise<Awaited<T>[]>

    • Like PromiseConstructor.allSettled, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • signal: AbortSignal | undefined
      • values: T

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

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

      Type Parameters

      • T

      Parameters

      • signal: AbortSignal | undefined
      • values: Iterable<T | PromiseLike<T>>

        An array of Promises.

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

      A new Promise.

    • Like PromiseConstructor.allSettled, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

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

    • Like PromiseConstructor.allSettled, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T

      Parameters

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

    • Like PromiseConstructor.any, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • signal: AbortSignal | undefined
      • values: T

      Returns Promise<Awaited<T[number]>>

    • 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

      • signal: AbortSignal | undefined
      • values: Iterable<T | PromiseLike<T>>

        An array or iterable of Promises.

      Returns Promise<Awaited<T>>

      A new Promise.

    • Like PromiseConstructor.any, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

      Returns Promise<Awaited<T[number]>>

    • Like PromiseConstructor.any, with an optional abort signal as the first argument.

      When a signal is provided, the returned promise is rejected with signal.reason if the signal is aborted before the returned promise settles.

      Type Parameters

      • T

      Parameters

      Returns Promise<Awaited<T>>

    • Returns a promise that resolves after the given duration.

      If a signal is provided and is aborted before the duration has elapsed, then the promise is rejected with signal.reason.

      Parameters

      • signal: AbortSignal | undefined
      • duration: number

        the duration to wait in milliseconds

      Returns Promise<void>

    • Returns a promise that resolves after the given duration.

      If a signal is provided and is aborted before the duration has elapsed, then the promise is rejected with signal.reason.

      Parameters

      • duration: number

        the duration to wait in milliseconds

      Returns Promise<void>

    • Wraps a promise with an abort signal.

      If the signal is aborted before the promise settles, then the returns promise is rejected with signal.reason. Otherwise, it settles in the same way as the original promise.

      If no signal is provided then the original promise is returned unchanged.

      Type Parameters

      • T

      Parameters

      • signal: AbortSignal | undefined
      • promise: Promise<T>

      Returns Promise<T>