A full polyfill for the Promise class. Represents the completion of an asynchronous operation, and its resulting value.

0.5.0

Constructors

Properties

Methods

Polyfill

Constructors

Properties

state?: string

Returns a string representation of the current state of the promise. The promise can be in one of four states.

  • "pending": The promise is not yet in a completed state (neither "rejected"; or "resolved").
  • "resolved": The promise is in the resolved state.
  • "rejected": The promise is in the rejected state.
let doResolve;
let promise: IPromise<any> = createSyncPromise((resolve) => {
doResolve = resolve;
});

let state: string = promise.state();
console.log("State: " + state); // State: pending
doResolve(true); // Promise will resolve synchronously as it's a synchronous promise
console.log("State: " + state); // State: resolved

Methods

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

    Type Parameters

    • TResult = never

    Parameters

    Returns IPromise<T | TResult>

    A Promise for the completion of the callback.

    const promise1 = createPromise((resolve, reject) => {
    throw 'Uh-oh!';
    });

    promise1.catch((error) => {
    console.error(error);
    });
    // expected output: Uh-oh!
  • Attaches a callback for only the rejection of the Promise.

    Type Parameters

    • TResult = never

    Parameters

    Returns PromiseLike<T | TResult>

    A Promise for the completion of the callback.

    const promise1 = createPromise((resolve, reject) => {
    throw 'Uh-oh!';
    });

    promise1.catch((error) => {
    console.error(error);
    });
    // expected output: Uh-oh!
  • Attaches a callback for only the rejection of the Promise.

    Type Parameters

    • TResult = never

    Parameters

    Returns Promise<T | TResult>

    A Promise for the completion of the callback.

    const promise1 = createPromise((resolve, reject) => {
    throw 'Uh-oh!';
    });

    promise1.catch((error) => {
    console.error(error);
    });
    // expected output: Uh-oh!
  • 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: (() => void)

      The callback to execute when the Promise is settled (fulfilled or rejected).

        • (): void
        • Returns void

    Returns IPromise<T>

    A Promise for the completion of the callback.

    function doFunction() {
    return createPromise((resolve, reject) => {
    if (Math.random() > 0.5) {
    resolve('Function has completed');
    } else {
    reject(new Error('Function failed to process'));
    }
    });
    }

    doFunction().then((data) => {
    console.log(data);
    }).catch((err) => {
    console.error(err);
    }).finally(() => {
    console.log('Function processing completed');
    });

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.