What is util.promisify in Node.js?
Understand util.promisify in Node.js: convert error-first callback functions into promise-returning ones for clean async/await code, with examples.
Expected Interview Answer
util.promisify is a Node.js core utility that converts a callback-based function following the error-first convention into a function that returns a Promise, letting you use it with async/await.
Many older Node APIs take a callback as the last argument, invoked as callback(err, result). util.promisify(fn) wraps such a function so calling it returns a Promise that rejects with err or resolves with result. This lets you replace nested callbacks with clean async/await. Functions can also expose a custom promisified version via the util.promisify.custom symbol, which promisify uses instead of the default wrapping when present.
- Turns callback APIs into Promise-returning ones
- Enables async/await on legacy functions
- Removes deeply nested callback pyramids
- Standardizes error handling with try/catch
- No external library needed — built into core
AI Mentor Explanation
util.promisify is like fitting an old manually-scored match with an electronic scoreboard adapter. The scorer still records runs the traditional way, but the adapter now flashes each result on the big screen automatically, so the whole ground reads updates cleanly instead of waiting for shouted callouts.
Step-by-Step Explanation
Step 1
Identify a callback API
Find a function whose last argument is an error-first callback, e.g. fs.readFile(path, cb).
Step 2
Import util.promisify
Require it from the core util module: const { promisify } = require('util').
Step 3
Wrap the function
Create a promise-returning version: const readFile = promisify(fs.readFile).
Step 4
Await the result
Call it inside an async function with await and wrap it in try/catch for errors.
Step 5
Prefer native promise APIs
Where available, use built-in promise variants like fs.promises instead of promisifying manually.
What Interviewer Expects
- The error-first callback convention it relies on
- That it returns a Promise-returning function, not a Promise
- How it pairs with async/await and try/catch
- Awareness of util.promisify.custom for non-standard signatures
- Knowing native promise APIs (fs.promises) often make it unnecessary
Common Mistakes
- Promisifying a function that does not use the error-first callback style
- Expecting promisify to return a Promise instead of a wrapped function
- Forgetting to bind methods that rely on their 'this' context
- Using it on functions whose callback returns multiple result values
- Reaching for it when a native promise API already exists
Best Answer (HR Friendly)
“util.promisify is a small built-in Node helper that upgrades old-style functions that use callbacks so they return promises instead. That lets developers write cleaner, more readable async code using async/await rather than nesting many callbacks.”
Code Example
const fs = require('fs');
const { promisify } = require('util');
// Wrap the callback-based function
const readFile = promisify(fs.readFile);
async function loadConfig() {
try {
const data = await readFile('./config.json', 'utf8');
return JSON.parse(data);
} catch (err) {
console.error('Failed to read config:', err.message);
throw err;
}
}
loadConfig().then((cfg) => console.log(cfg));
// Custom promisified behavior via the special symbol
function getUser(id, cb) {
cb(null, { id, name: 'Ada' });
}
getUser[promisify.custom] = (id) =>
Promise.resolve({ id, name: 'Ada' });
const getUserAsync = promisify(getUser);Follow-up Questions
- What is the error-first callback convention?
- How does util.promisify.custom change the wrapping behavior?
- When would you use fs.promises instead of promisify?
- Can you promisify a function that returns multiple values in its callback?
- How does async/await improve on nested callbacks?
MCQ Practice
1. What does util.promisify return?
promisify(fn) gives you a new function; calling that function returns a Promise, it does not return a Promise itself.
2. util.promisify expects the wrapped function to use which convention?
It assumes the last argument is a callback invoked as callback(err, result), the standard Node error-first style.
3. How can a function control how promisify wraps it?
If a function defines the util.promisify.custom symbol, promisify uses that implementation instead of the default wrapping.
Flash Cards
What does util.promisify do? — Converts an error-first callback function into one that returns a Promise for use with async/await.
Does promisify return a Promise? — No — it returns a new function; that function returns a Promise when called.
What convention must the callback follow? — Error-first: callback(err, result).
What is util.promisify.custom? — A symbol a function can define to provide its own promisified implementation.