QA
Beginner LevelNode JS

How does util.promisify() convert error-first callback functions into Promises?

❓ Interview Prompt: Explain `node:util` module, error-first callback pattern `(err, data) => {}`, and converting functions via `util.promisify()`.

Node.js traditionally used the Error-First Callback pattern '(err, data) => {}'. 'util.promisify()' wraps callback functions to return standard Promises:

Code Implementation Example

javascript
import util from 'node:util';
import child_process from 'node:child_process';

// Convert callback-based exec to Promise-based exec!
const execPromise = util.promisify(child_process.exec);

async function getSystemUptime() {
  const { stdout } = await execPromise('uptime');
  console.log('System Uptime:', stdout.trim());
}
💡Converting callback functions to promises using util.promisify.
🎯 Interview Key Takeaway

Use util.promisify to convert legacy callback functions into clean async/await promises.

#promisify#util#Promises#Callbacks#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?