Promise.withResolvers for Event Queues, Streams, and Callback Interop
Use externally available resolve and reject functions to bridge callback-driven events into a promise lifecycle.
Promise.withResolvers() returns a promise together with the functions that settle it. It is useful when a promise must be created in one scope and settled later by an event, callback, or queue, without hiding the resolver inside a constructor callback.
THE PRODUCTION PROBLEM STATEMENT
Some APIs are event-driven rather than promise-based. Wrapping them often leads to nested executor state or an awkward callback-to-promise bridge that is difficult to reset safely.
Create a promise whose settlement belongs to another scope
The returned object contains promise, resolve, and reject. This makes the relationship explicit when setup happens now but completion arrives from a later event. The resolver still follows normal Promise rules: the first settlement wins and later calls do nothing.
Use this shape when it clarifies ownership, not as a general replacement for async functions. A promise that can remain pending forever still needs a timeout, cancellation path, or lifecycle owner.
function waitForMessage(target, type, { signal } = {}) {
const { promise, resolve, reject } = Promise.withResolvers();
const cleanup = () => target.removeEventListener(type, onMessage);
const onMessage = (event) => { cleanup(); resolve(event); };
target.addEventListener(type, onMessage, { once: true });
signal?.addEventListener('abort', () => {
cleanup();
reject(signal.reason);
}, { once: true });
return promise;
}Use a fresh capability for each queue cycle
A queue can keep a promise pending while producers add work, then resolve it when an item arrives. After resolving one cycle, replace the promise and its resolver for the next empty interval. Reusing an already-settled promise cannot represent a future wait.
Every event registration needs a corresponding cleanup path. If the consumer cancels, remove listeners and detach the pending waiter so the queue does not retain abandoned closures.
Keep the resolver private to the lifecycle owner
A resolver is a capability: whoever holds it can complete the operation. Keep it in the queue or adapter that owns the lifecycle instead of exposing resolve to arbitrary callers.
Promise.withResolvers() is standardized and widely available in modern runtimes, but older embedded browsers may need a small constructor-based compatibility helper. Feature-detect when your support matrix requires that fallback.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
It creates an equivalent promise and returns its resolve and reject functions alongside it, without capturing them from an executor callback.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
The JavaScript Event Loop in Depth: Microtasks, Macrotasks, and Browser Frame Budgeting
An exhaustive dissection of V8 execution contexts, microtask queue drainage, task scheduling, requestAnimationFrame, and preventing main-thread UI jank.
Mastering JavaScript Iterators, Async Generators, and Reactive Stream Processing
How to process unbounded data streams with Symbol.iterator and Symbol.asyncIterator, implement backpressure, and consume NDJSON chunk by chunk.