Promise.withResolvers, and a deferred I had hand-rolled

The deferred pattern — a promise whose resolve and reject escape the executor — written by hand in every project that needs it.

// before
let resolve, reject
const promise = new Promise((res, rej) => { resolve = res; reject = rej })

// ES2024
const { promise, resolve, reject } = Promise.withResolvers()

// where it is actually needed: a queue of pending
// requests keyed by id, resolved when a message
// arrives over a socket.

The hand-rolled version works and needs a comment explaining that the assignments happen synchronously, which is the part that looks wrong. The genuine use is narrow — a promise resolved by something outside the function that created it, which is almost always a message-passing boundary — and reaching for it elsewhere is usually a sign the promise should have been created closer to the work.