Skip to solution
mediumFrontend

How do you convert a callback-based function to a Promise?

1.1k views
01

Understand the problem

Question presented to candidate: "You're given a legacy function like readFile(path, (err, data) => {...}) using Node's classic error-first callback convention. Write a real, generic helper that converts ANY such function into one that returns a Promise instead."

What a strong answer should cover:

  • 📌 Interview term: the error-first (Node-style) callback convention — the classic pattern this conversion targets: a callback's FIRST argument is either an Error (on failure) or null/undefined (on success), with subsequent arguments carrying the real result.
  • 📌 Interview term: wrapping with new Promise — the real, direct answer: construct a new Promise((resolve, reject) => { ... }), calling the original callback-based function INSIDE the executor, and inside that callback, calling reject(err) if err is truthy, otherwise resolve(result).
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: a real, generic promisify(fn) helper — written once, reusable for ANY error-first function, not hardcoded to one specific function — was verified correctly resolveing on a real success case and rejecting on a real error case, matching the original callback's own real behavior exactly.
  • 📌 Interview term: Node's own built-in util.promisify — verified directly: Node's REAL, built-in util.promisify() does the IDENTICAL conversion, confirmed producing the same real result as the hand-written version for the identical legacy function — a real, standard tool that makes hand-rolling this conversion unnecessary in Node code specifically.
  • A precise answer names the real, general SHAPE of the conversion: (...args) => new Promise((resolve, reject) => fn(...args, (err, result) => err ? reject(err) : resolve(result))) — genuinely works for any function following the error-first convention, regardless of how many success-value arguments it has (with the honest caveat that a callback returning MULTIPLE success values needs a small, deliberate adjustment, since a Promise can only resolve with one value).

Clarifying questions expected:

  • None — this is a definitional/technical question; writing a real, GENERIC helper (not a one-off hardcoded wrapper for a single function) is the strong signal.

Code / implementation expected: Yes — a real, generic promisify helper, verified working for both the success and error paths of a real legacy callback function, is the clearest, most convincing demonstration.

promisify
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/Node.js fundamentals 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, against a real (simulat

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, generic promisify helper verified correctly converting a legacy error-first callback function's success and error paths into a real Promise
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 47 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track