Represents the completion of an asynchronous operation
Symbol
PromiseConstructor
interface PromiseConstructor
- constructor <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>
Creates a new Promise.
@param executorA 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.
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 valuesAn iterable of Promises.
@returnsA 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 valuesAn array of Promises.
@returnsA 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 valuesAn array of Promises.
@returnsA new Promise.
Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.
@param valuesAn array of Promises.
@returnsA 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.
@param valuesAn array or iterable of Promises.
@returnsA 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.
@param valuesAn array or iterable of Promises.
@returnsA new Promise.
Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
@param valuesAn iterable of Promises.
@returnsA new Promise.
Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
@param valuesAn array of Promises.
@returnsA new Promise.
Creates a new rejected promise for the provided reason.
@param reasonThe reason the promise was rejected.
@returnsA new rejected Promise.
Creates a new resolved promise for the provided value.
@param valueA promise.
@returnsA promise whose internal state matches the provided promise.
Creates a new resolved promise for the provided value.
@param valueA promise.
@returnsA 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 callbackFnA function that is called synchronously. It can do anything: either return a value, throw an error, or return a promise.
@param argsAdditional arguments, that will be passed to the callback.
@returnsA 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 to run a function and return the result. If the function throws, return the result of the
catch
function.@param fnThe function to run
@param argsThe arguments to pass to the function. This is similar to
setTimeout
and avoids the extra closure.@returnsThe result of the function or the result of the
catch
function Creates a new Promise and returns it in an object, along with its resolve and reject functions.
@returnsAn object with the properties
promise
,resolve
, andreject
.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
andreject
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.