Intermediate Node.js Interview QuestionsIntermediateConcept
Node.js · Question 61
What is the difference between callbacks and Promises in Node.js?
Direct answer
Callbacks (err, result) nest and require manual error propagation at each step; Promises chain with .then/catch or async/await with centralized error handling — modern Node favors Promises and fs.promises, but libuv and streams still expose callback and event APIs underneath.
Callbacks were Node's original async style — Promises (ES2015) and async/await layered on top without removing the event-loop + callback foundation.
| Aspect | Callbacks | Promises |
|---|---|---|
| Error handling | if (err) at every level | .catch / try/catch once |
| Composition | Callback hell / pyramid | Chain or Promise.all |
| Cancellation | Manual flags | AbortSignal (modern) |
| Node APIs | Legacy fs, some crypto | fs/promises, fetch, pipeline |
- Error-first convention — (err, data) =>; omitting err check is a classic bug.
- Callback hell — nested readFile → parse → query → respond.
- Promisify bridge — Q59 connects legacy to modern style.
callback-vs-promise.mjs
// Callback style
fs.readFile("config.json", "utf8", (err, raw) => {
if (err) return console.error(err);
const config = JSON.parse(raw);
db.connect(config.url, (err2) => {
if (err2) return console.error(err2);
// ...
});
});
// Promise style
const raw = await fs.readFile("config.json", "utf8");
const config = JSON.parse(raw);
await db.connect(config.url);