A promise controlled by a callback bridges two styles of API. Keep its resolve and reject functions scoped to the operation that owns them.
Worked example
const { promise, resolve } = Promise.withResolvers();
queueMicrotask(() => resolve("ready"));
console.log(await promise);Result
readyImplementation notes
The external callback can outlive the listener that created the promise. Clean up listeners and define a timeout where a completion event is optional.
Try it yourself
Call the callback twice and verify that the settled value stays fixed.
Open the JavaScript example →Editorial date September 10, 2025 · Published December 6, 2026