Interface PolyPromiseConstructor

The PolyPromiseConstructor interface represents the constructor for the polyfill Promise object.

0.5.0

interface PolyPromiseConstructor {
    all<T>(input: Iterable<PromiseLike<T>>, timeout?: number): IPromise<T[]>;
    allSettled<T>(values: T, timeout?: number): Promise<{
        -readonly [P in string | number | symbol]: IPromiseResult<Awaited<T[P<P>]>>
    }>;
    allSettled<T>(values: Iterable<T | PromiseLike<T>>, timeout?: number): IPromise<IPromiseResult<Awaited<T>>[]>;
    any<T>(values: Iterable<T | PromiseLike<T>>, timeout?: number): IPromise<Awaited<T>>;
    any<T>(values: T, timeout?: number): IPromise<Awaited<T[number]>>;
    new PolyPromiseConstructornew <T>(executor: PromiseExecutor<T>): IPromise<T>;
    race<T>(values: Iterable<T | PromiseLike<T>>, timeout?: number): IPromise<Awaited<T>>;
    race<T>(values: T, timeout?: number): IPromise<Awaited<T[number]>>;
    reject<T>(reason?: any, timeout?: number): IPromise<T>;
    resolve(): IPromise<void>;
    resolve<T>(value: T | PromiseLike<T>, timeout?: number): IPromise<T>;
}

Constructors

  • Creates a new Promise.

    Type Parameters

    • T

    Parameters

    • executor: PromiseExecutor<T>

      A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.

    Returns IPromise<T>

Polyfill

  • Returns a single asynchronous Promise instance that resolves to an array of the results from the input promises. This returned promise will resolve and execute it's pending chained operations asynchronously using the optional provided timeout value to schedule when the chained items will be executed, or if the input contains no promises. It rejects immediately upon any of the input promises rejected or non-promises throwing an error, and will reject with this first rejection message / error. When resolved or rejected any additional chained operations will execute asynchronously using the optional timeout value to schedul when the chained item will be executed (eg. then(); catch(); finally()).

    Type Parameters

    • T

    Parameters

    • input: Iterable<PromiseLike<T>>

      The array of promises to wait to be resolved / rejected before resolving or rejecting the new promise

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero.

    Returns IPromise<T[]>

    • An already resolved `Promise`, if the input passed is empty.
    • A pending `Promise` in all other cases. This returned promise is then resolved/rejected __synchronously__ (as soon as the pending items is empty) when all the promises in the given input have resolved, or if any of the promises reject.
  • Returns a single Promise instance that resolves to an array of the results from the input promises. This returned promise will resolve and execute it's pending chained operations based on the Aasynchronous promise implementation. Any chained operations will execute asynchronously when the final operation pending promises have resolved, or if the input contains no promises. It will resolve only after all of the input promises have either resolved or rejected, and will resolve with an array of IPromiseResult objects that each describe the outcome of each promise.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An array of promises to wait to be resolved / rejected before resolving or rejecting the new promise

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

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

    A pending Promise that will resolve to an array of IPromiseResult objects that each describe the outcome of each promise.

    0.5.0

  • Returns a single Promise instance that resolves to an array of the results from the input promises. This returned promise will resolve and execute it's pending chained operations based on the Aasynchronous promise implementation. Any chained operations will execute asynchronously when the final operation pending promises have resolved, or if the input contains no promises. It will resolve only after all of the input promises have either resolved or rejected, and will resolve with an array of IPromiseResult objects that each describe the outcome of each promise.

    Type Parameters

    • T

    Parameters

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

      An array of promises to wait to be resolved / rejected before resolving or rejecting the new promise

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

    Returns IPromise<IPromiseResult<Awaited<T>>[]>

    A pending Promise that will resolve to an array of IPromiseResult objects that each describe the outcome of each promise.

    0.5.0

  • The createAsyncAnyPromise method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed), with an AggregateError containing an array of rejection reasons.

    Type Parameters

    • T

    Parameters

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

      An iterable object of promises.

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

    Returns IPromise<Awaited<T>>

    A new Promise that is:

    • Already rejected, if the iterable passed is empty.
    • Asynchronously fulfilled, when any of the promises in the given iterable fulfills. The fulfillment value is the fulfillment value of the first promise that was fulfilled.
    • Asynchronously rejected, when all of the promises in the given iterable reject. The rejection reason is an AggregateError containing an array of rejection reasons in its errors property. The errors are in the order of the promises passed, regardless of completion order. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) rejected.

    0.5.0

  • The createAsyncAnyPromise method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed), with an AggregateError containing an array of rejection reasons.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An iterable object of promises.

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

    Returns IPromise<Awaited<T[number]>>

    A new Promise that is:

    • Already rejected, if the iterable passed is empty.
    • Asynchronously fulfilled, when any of the promises in the given iterable fulfills. The fulfillment value is the fulfillment value of the first promise that was fulfilled.
    • Asynchronously rejected, when all of the promises in the given iterable reject. The rejection reason is an AggregateError containing an array of rejection reasons in its errors property. The errors are in the order of the promises passed, regardless of completion order. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) rejected.

    0.5.0

  • The createAsyncRacePromise method takes an array of promises as input and returns a single Promise. This returned promise settles with the eventual state of the first promise that settles.

    Type Parameters

    • T

    Parameters

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

      An iterable object of promises.

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

    Returns IPromise<Awaited<T>>

    A Promise that settles with the eventual state of the first promise in the iterable to settle. In other words, it fulfills if the first promise to settle is fulfilled, and rejects if the first promise to settle is rejected. The returned promise remains pending forever if the iterable passed is empty. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously settled.

    The createAsyncRacePromise method is one of the promise concurrency methods. It's useful when you want the first async task to complete, but do not care about its eventual state (i.e. it can either succeed or fail). If the iterable contains one or more non-promise values and/or an already settled promise, then Promise.race() will settle to the first of these values found in the iterable.

    0.5.0

  • The createAsyncRacePromise method takes an array of promises as input and returns a single Promise. This returned promise settles with the eventual state of the first promise that settles.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An iterable object of promises.

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero, only used when Native promises are not available.

    Returns IPromise<Awaited<T[number]>>

    A Promise that settles with the eventual state of the first promise in the iterable to settle. In other words, it fulfills if the first promise to settle is fulfilled, and rejects if the first promise to settle is rejected. The returned promise remains pending forever if the iterable passed is empty. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously settled.

    The createAsyncRacePromise method is one of the promise concurrency methods. It's useful when you want the first async task to complete, but do not care about its eventual state (i.e. it can either succeed or fail). If the iterable contains one or more non-promise values and/or an already settled promise, then Promise.race() will settle to the first of these values found in the iterable.

    0.5.0

  • Returns a single asynchronous Promise instance that is already rejected with the given reason. Any chained operations will execute asynchronously using the optional timeout value to schedule when then chained items will be executed. (eg. catch(); finally()).

    Type Parameters

    • T = never

    Parameters

    • Optionalreason: any

      The rejection reason

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero.

    Returns IPromise<T>

    A rejected promise.

  • Returns a single asynchronous Promise instance that is already resolved with the given value. If the value passed is a promise then that promise is returned instead of creating a new asynchronous promise instance. If a new instance is returned then any chained operations will execute asynchronously using the optional timeout value to schedule when the chained items will be executed.(eg. then(); finally()).

    Returns IPromise<void>

    A resolved promise.

  • Returns a single asynchronous Promise instance that is already resolved with the given value. If the value passed is a promise then that promise is returned instead of creating a new asynchronous promise instance. If a new instance is returned then any chained operations will execute asynchronously using the optional timeout value to schedule when the chained items will be executed.(eg. then(); finally()).

    Type Parameters

    • T

    Parameters

    • value: T | PromiseLike<T>

      The value to be used by this Promise. Can also be a Promise or a thenable to resolve.

    • Optionaltimeout: number

      Optional timeout to wait before processing the items, defaults to zero.

    Returns IPromise<T>

    A resolved promise.