Promise.withResolvers
Returns an object containing a new Promise and two functions to resolve or reject it, corresponding to the parameters of the Promise() constructor
Syntax
Promise.withResolvers<T>()Return Value
An object with properties: promise, resolve, reject
Examples
const { promise, resolve, reject } = Promise.withResolvers<string>();
setTimeout(() => resolve('done'), 1000);
const result = await promise;
console.log(result); // 'done'function createDeferred<T>() {
const { promise, resolve, reject } = Promise.withResolvers<T>();
return { promise, resolve, reject };
}
const deferred = createDeferred<number>();
deferred.resolve(42);const { promise, resolve } = Promise.withResolvers<void>();
document.getElementById('btn')?.addEventListener('click', () => resolve());
await promise;
console.log('Button clicked!');Understanding Promise.withResolvers
The Promise.withResolvers method in JavaScript returns an object containing a new Promise and two functions to resolve or reject it, corresponding to the parameters of the Promise() constructor. It belongs to the Promise object and is one of the most widely used methods for working with promise values in modern JavaScript and TypeScript applications.
The method signature is Promise.withResolvers<T>(). When called, it returns an object with properties: promise, resolve, reject. Understanding when and how to use withResolvers() helps you write more expressive, readable code.
Common use cases for Promise.withResolvers include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like promise-resolve, promise-reject, promise-then, enabling you to chain operations together for complex data manipulation pipelines.
Supported in Chrome 119+, Firefox 121+, Safari 17.2+, Edge 119+, and Node.js 22+.
Browser Compatibility
Supported in Chrome 119+, Firefox 121+, Safari 17.2+, Edge 119+, and Node.js 22+.
Related Methods
More Promise Methods
Other methods in the Promise object
Related Tools
More Promise Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.