Newly availableNewly available. Not yet supported in older browsers; verify your target environments before adopting.

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
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

Promise.withResolvers

Promise 1 row in create. with part from resolve / reject.

PreviewFullscreen

traditional. side notation and. comparison

Promise constrakta. middle in resolve getoutbetween that abbreviate.

PreviewFullscreen

Usage with Eventmove.

buttonclickequal. partevent Promise ization.

PreviewFullscreen

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.

Powered by web-features