@azerum/ts-csp
    Preparing search index...

    Class AbortablePromise<T>

    Extends Promise constructor to accept not just a (resolve, reject) => void function, but (resolve, reject) => cleanupOnAbortFn and AbortSignal

    Helps to write promises that can be cancelled without leaking abort listeners on the signal, and without forgetting to write cleanup logic. See the example below

    Semantics:

    • Adds abort listener on AbortSignal. After the promise settles, the listener is always removed (no leaks)

    • If abort fires before resolve or reject is called, calls cleanupOnAbort function is called and rejects with AbortedError

    • If resolve or reject is called before abort is fired, resolves/rejects as a regular promise

    • cleanupOnAbort is guaranteed to be called at most once, and only if resolve or reject has not been called yet

    • Race condition is possible where abort fires, than resolve or reject is performed before abort is handled. In such case resolve/reject always wins.

    • If passed AbortSignal is already aborted, does not call executor at all, and rejects with AbortedError

    Promisified setTimeout with cancellation that removes the timer

    function sleep(ms: number, signal?: AbortSignal) {
    return new AbortablePromise((resolve, _reject) => {
    const handle = setTimeout(() => resolve(), ms)

    // Called only if `signal` is aborted before `setTimeout` callback
    // is executed
    return () => clearTimeout(ms)
    }, signal)
    }

    Type Parameters

    • T

    Hierarchy

    • Promise<T>
      • AbortablePromise
    Index

    Constructors

    • Type Parameters

      • T

      Parameters

      • executor: (
            resolve: (value: T) => void,
            reject: (exception: unknown) => void,
        ) => () => void
      • Optionalsignal: AbortSignal

      Returns AbortablePromise<T>

    Properties

    "[toStringTag]": string
    "[species]": PromiseConstructor

    Methods

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

    • 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

      • 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

      • value: T | PromiseLike<T>

        A promise.

      Returns Promise<Awaited<T>>

      A promise whose internal state matches the provided promise.