Dev Helpers

JavaScript / Streams and asynchronous work

Resolve a promise through an external callback

Dev Helpers · September 10, 2025

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

ready

Implementation 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

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.