Skip to solution
mediumFrontend

What does Promise.withResolvers() solve and how does it replace the deferred anti-pattern?

215 views
01

Understand the problem

Question presented to candidate: "Before Promise.withResolvers existed, if you needed to resolve a Promise from OUTSIDE its executor function — say, from an event listener registered elsewhere — how would you have done it? What does Promise.withResolvers actually give you that's different?"

What a strong answer should cover:

  • 📌 Interview term: Promise.withResolvers() — a static ES2024 method returning a real object { promise, resolve, reject } in ONE call — verified directly: promise is a genuine Promise instance, and resolve/reject are real, working functions that settle it from anywhere.
  • 📌 Interview term: the real, direct answer to the prompt's historical question — the classic pre-withResolvers "deferred" pattern: manually declaring let resolveFn, rejectFn; OUTSIDE a new Promise((res, rej) => { resolveFn = res; rejectFn = rej; }) executor, capturing them via closure — verified directly to have the IDENTICAL real shape/capability as Promise.withResolvers(), just requiring several manual lines instead of one call.
  • 📌 Interview term: the real, practical use case — verified directly: resolving a Promise from inside an external, event-callback-style function (simulating an event listener registered elsewhere, entirely outside any executor) — exactly the scenario the prompt describes, confirmed working correctly.
  • A precise answer names that Promise.withResolvers() does not add any NEW capability beyond the deferred pattern — both genuinely expose resolve/reject outside the executor — its real value is ergonomic: one direct call versus manually declaring, capturing, and correctly typing several separate variables.
  • A precise answer names a genuinely common real use case beyond events: bridging a callback-based API (or any external system that "calls back" later) into a single Promise, without needing to wrap the ENTIRE calling code inside a new Promise executor.

Clarifying questions expected:

  • None — this is a definitional/historical-technical question; directly naming and reproducing the deferred anti-pattern it replaces is the strong signal.

Code / implementation expected: Yes — real, side-by-side proof of the classic manual deferred pattern next to Promise.withResolvers()'s identical shape, plus the real external-event use case, is the clearest demonstration.

promiseses2024async
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript/async interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every resolve/reject path below was actually run in Node (ES2024, Node 20.12+).

1. Why This

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, side-by-side proof: Promise.withResolvers() has the identical shape as the classic manual deferred pattern, plus a real external-callback use case
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 108 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track