Node.js › Async Patterns

What is util.promisify(), and when would you still need it in modern Node.js?

Intermediate

Answer

util.promisify() converts a traditional Node.js error-first callback-style function into one that returns a Promise instead, letting it be used with async/await -- this remains useful when working with older core APIs or third-party libraries that haven't yet adopted the newer promise-based APIs that Node.js has increasingly added natively (like fs.promises or node:timers/promises).
const util = require('node:util');
const fs = require('node:fs');

const readFileAsync = util.promisify(fs.readFile);
const data = await readFileAsync('config.json', 'utf-8');

// Modern alternative: many core modules now ship promise versions directly
const fsPromises = require('node:fs/promises');
const data2 = await fsPromises.readFile('config.json', 'utf-8');
Real-world example A codebase depending on an older third-party callback-based Redis client wraps its get/set methods with util.promisify() to use them consistently with the rest of an async/await-based application, without needing to switch Redis client libraries just to gain promise support.

Common follow-ups: What convention must a callback-based function follow for util.promisify() to work correctly on it?;How do you handle a callback-based function that doesn't follow the standard error-first convention?