Promise.withResolvers()
The Promise.withResolvers() static method returns an object containing a new Promise object and two functions to resolve or reject it, corresponding to the two parameters passed to the executor of the Promise() constructor.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 119 | 119 | 121 | 17.4 | 119 | 17.4 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
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.
PreviewFullscreen
Use cases
-
Using Promise.withResolvers()
The Promise.
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.