Skip to the solution
MediumFrontend6 min read

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

1.1k views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, generic promisify helper verified correctly converting a legacy error-first callback function's success and error paths into a real Promise

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 47 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Explain 'this' keyword in JavaScript.

Continue