MwithResolvers
Bun

method

PromiseConstructor.withResolvers

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.

const { promise, resolve, reject } = Promise.withResolvers();

setTimeout(() => {
 resolve("Hello world!");
}, 1000);

await promise; // "Hello world!"