Full answer
In JavaScript, functions are values, so one function can receive another and call it when needed. That received function is a callback. A synchronous callback, such as the one given to map, is called before the outer function returns. An asynchronous callback is called later, after work such as a timer, a file read, or a network request has finished.
Node.js popularized the error-first convention: the callback receives (error, result), and the code checks error first. Every step must handle errors itself, and a throw inside an asynchronous callback cannot be caught by a try/catch around the outer call.
Callback hell appears when each step depends on the previous result and is nested inside it. The code drifts to the right, error handling is repeated at every level, and sharing data between steps is awkward. A deeper problem is inversion of control: you hand your function to code you do not control, and trust it to call back once, with the right arguments, at the right time.
There are several ways out. Name the functions and keep them at the top level, which flattens the nesting. Wrap callback APIs in promises, so steps chain with then and errors flow to one catch. Use async/await for code that reads in order. In Node.js, util.promisify turns an error-first function into one that returns a promise.
Callbacks are still fine for events and simple cases, such as addEventListener.
const { promisify } = require('node:util');
function loadStudent(id, callback) {
setTimeout(() => {
if (id < 0) callback(new Error('invalid id'));
else callback(null, { id, name: 'Asha' });
}, 5);
}
function loadMarks(student, callback) {
setTimeout(() => callback(null, { ...student, marks: 88 }), 5);
}
// Nested callbacks: each step sits inside the one before it.
loadStudent(1, (error, student) => {
if (error) return console.log('nested error:', error.message);
loadMarks(student, (error2, withMarks) => {
if (error2) return console.log('nested error:', error2.message);
console.log('nested result:', withMarks);
});
});
// The same flow with promisified functions and async/await.
const loadStudentAsync = promisify(loadStudent);
const loadMarksAsync = promisify(loadMarks);
(async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
try {
const student = await loadStudentAsync(1);
console.log('flat result:', await loadMarksAsync(student));
await loadStudentAsync(-1);
} catch (error) {
console.log('one catch for every step:', error.message);
}
try {
setTimeout(() => {
throw new Error('thrown later');
}, 0);
} catch (error) {
console.log('never printed');
}
})();
process.on('uncaughtException', (error) => console.log('uncaught outside try/catch:', error.message));Run with Node.js v24.22.1 as js-60.js; two runs gave identical output. Output: nested result: { id: 1, name: 'Asha', marks: 88 } flat result: { id: 1, name: 'Asha', marks: 88 } one catch for every step: invalid id uncaught outside try/catch: thrown later
Key points
- A callback is a function passed in to be called later, sync or async.
- Error-first style: callback(error, result).
- try/catch cannot catch errors thrown later inside an async callback.
- Callback hell is deep nesting plus repeated error handling and inversion of control.
- Fixes: named functions, promises, async/await, and promisify.
Common mistakes
- Wrapping an asynchronous call in try/catch and expecting it to catch errors from the callback.
- Calling a callback twice, for example after both a success path and an error path.
- Mixing styles: a function that sometimes calls back synchronously and sometimes asynchronously.
- Nesting more than two levels without extracting named functions.
Follow-ups
- How would you write promisify yourself?
- What is the difference between a synchronous and an asynchronous callback in terms of when errors surface?
- Why is it a problem when an API calls its callback sometimes synchronously and sometimes later?
Tips
- Define a callback, then give both a sync and an async example.
- Mention inversion of control; it is the deeper problem behind callback hell.
- Show promisify or a manual promise wrapper as the bridge between old and new styles.