Promise.withResolvers()
The Promise.withResolvers() static method is an alternative to the Promise() constructor that returns both the promise and resolution functions. You can use this to access resolve and reject outside the scope of the executor function.
Overview
The Promise.withResolvers() static method is an alternative to the Promise() constructor that returns both the promise and resolution functions. You can use this to access resolve and reject outside the scope of the executor function.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 119 | 119 | 121 | 17.4 | 119 | 17.4 | |
Syntax
const { promise, resolve, reject } = Promise.withResolvers();
// Resolve from an event handler
button.addEventListener('click', () => resolve('clicked'));
setTimeout(() => reject(new Error('timeout')), 5000);
const result = await promise; Live demo
traditional. side notation and. comparison
Promise constrakta. middle in resolve getoutbetween that abbreviate.
Use cases
Using Promise.withResolvers()
The Promise.withResolvers() static method is an alternative to the Promise() constructor that returns both the promise and resolution functions. You can use this to access resolve and reject outside the scope of the executor function.
Cautions
- Not supported in older browsers. Verify your target environments before adopting.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features