liveBook · Manning

Promise.withResolvers

As explained in Secrets of the JavaScript Ninja, Third Edition

Promise.withResolvers is a static JavaScript method that returns a new promise together with its resolve and reject functions.

When a callback-based API needs to settle a promise, Promise.withResolvers() returns a new promise together with its resolve and reject functions. Resolution is one-shot: once resolved, later calls to either function silently fail. section 8.1.1

Expose settlement functions

const { promise, resolve, reject } = Promise.withResolvers(); // Pass resolve as a callback to connect a callback-based API to the promise. section 8.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines the returned promise and settlement functions, and notes one-shot resolution without detailing the full resolution algorithm.
Examples
Uses destructuring to obtain a promise and its resolve and reject functions for a callback-based API.
What you can do after
Apply the native method to expose promise settlement functions for use as callbacks.
How it connects
Connects promises with callbacks, promise resolution, and thenables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.