PromiseConstructor

Bun

Symbol

PromiseConstructor

interface PromiseConstructor

Represents the completion of an asynchronous operation

  • constructor <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>

    Creates a new Promise.

    @param executor

    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.

  • readonly prototype: Promise<any>

    A reference to the prototype.

  • all<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>

    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.

    @param values

    An iterable of Promises.

    @returns

    A new Promise.

    all<T extends readonly unknown[] | []>(values: T): Promise<{ [K 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.

    @param values

    An array of Promises.

    @returns

    A new Promise.

  • allSettled<T extends readonly unknown[] | []>(values: T): Promise<{ [K 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.

    @param values

    An array of Promises.

    @returns

    A new Promise.

    allSettled<T>(values: Iterable<T | PromiseLike<T>>): Promise<PromiseSettledResult<Awaited<T>>[]>

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

    @param values

    An array of Promises.

    @returns

    A new Promise.

  • any<T extends readonly unknown[] | []>(values: T): 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.

    @param values

    An array or iterable of Promises.

    @returns

    A new Promise.

    any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>

    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.

    @param values

    An array or iterable of Promises.

    @returns

    A new Promise.

  • race<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>

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

    @param values

    An iterable of Promises.

    @returns

    A new Promise.

    race<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>

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

    @param values

    An array of Promises.

    @returns

    A new Promise.

  • reject<T = never>(reason?: any): Promise<T>

    Creates a new rejected promise for the provided reason.

    @param reason

    The reason the promise was rejected.

    @returns

    A new rejected Promise.

  • resolve(): Promise<void>

    Creates a new resolved promise.

    @returns

    A resolved promise.

    resolve<T>(value: T): Promise<Awaited<T>>

    Creates a new resolved promise for the provided value.

    @param value

    A promise.

    @returns

    A promise whose internal state matches the provided promise.

    resolve<T>(value: T | PromiseLike<T>): Promise<Awaited<T>>

    Creates a new resolved promise for the provided value.

    @param value

    A promise.

    @returns

    A promise whose internal state matches the provided promise.

  • try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>

    Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result in a Promise.

    @param callbackFn

    A function that is called synchronously. It can do anything: either return a value, throw an error, or return a promise.

    @param args

    Additional arguments, that will be passed to the callback.

    @returns

    A Promise that is:

    • Already fulfilled, if the callback synchronously returns a value.
    • Already rejected, if the callback synchronously throws an error.
    • Asynchronously fulfilled or rejected, if the callback returns a promise.
    try<T, A extends any[] = []>(fn: (...args: A) => T | PromiseLike<T>, ...args: A): Promise<T>

    Try to run a function and return the result. If the function throws, return the result of the catch function.

    @param fn

    The function to run

    @param args

    The arguments to pass to the function. This is similar to setTimeout and avoids the extra closure.

    @returns

    The result of the function or the result of the catch function

  • withResolvers<T>(): PromiseWithResolvers<T>

    Creates a new Promise and returns it in an object, along with its resolve and reject functions.

    @returns

    An object with the properties promise, resolve, and reject.

    const { promise, resolve, reject } = Promise.withResolvers<T>();
    
    withResolvers<T>(): { promise: Promise<T>; reject: (reason?: any) => void; resolve: (value?: T | PromiseLike<T>) => void }

    Create a deferred promise, with exposed resolve and reject methods which can be called separately.

    This is useful when you want to return a Promise and have code outside the Promise resolve or reject it.

    Example

    const { promise, resolve, reject } = Promise.withResolvers();
    
    setTimeout(() => {
     resolve("Hello world!");
    }, 1000);
    
    await promise; // "Hello world!"
    

    Promise.withResolvers() is a stage3 proposal.