method
PromiseConstructor.withResolvers
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 its resolve and reject functions exposed, so code outside the promise can settle it.
const { promise, resolve, reject } = Promise.withResolvers();
setTimeout(() => {
resolve("Hello world!");
}, 1000);
await promise; // "Hello world!"