A callback is a function you pass to another function so that it can be called later, usually when an asynchronous operation such as reading a file finishes. Callback hell (also called the pyramid of doom) is what happens when many dependent callbacks are nested inside each other, so the code drifts to the right and becomes hard to read, test and debug. Searching for "callback hell nodejs" usually means you have hit exactly this problem in your own code.
Node.js was built around callbacks, so understanding them is the base for promises and async/await, which were created to solve the nesting problem.
In JavaScript, functions are values. You can store them in variables and pass them as arguments. A function that receives another function and uses it is a higher-order function, and the function it receives is a callback.
Callbacks are not always asynchronous. This one runs immediately:
The callback passed to forEach is called synchronously, once per element. The callbacks that matter for Node.js are the ones called later.
Node.js does not wait for slow operations such as disk or network access. You start the operation, hand it a callback, and Node.js carries on with other work. When the operation completes, the callback is placed in a queue and runs once the call stack is free.
after is printed before the file content because readFile returns immediately. The callback runs later, when the data has been read. Never assume code written below an asynchronous call has the result of that call.
Real programs need steps that depend on each other: read a config file, use its value to read a second file, then write a result. Each step needs the output of the previous one, so each callback is placed inside the previous one.
With three steps this is still readable. With ten steps, plus loops, retries and cleanup, the code forms a pyramid. The problems are practical:
The cheapest fix is to move each callback into a named function. Nothing is nested more than one level.
Each function can now be read and tested on its own. Early return statements after an error keep the happy path unindented.
The modern answer is to use promise-based APIs. Node.js ships them in node:fs/promises, and async/await makes the steps read from top to bottom.
The same three steps are now flat, and a single catch handles an error from any of them. Related articles cover promises and async/await in detail.
If a library still uses callbacks, util.promisify wraps any function that follows the error-first convention and returns a promise.
Top-level await works in ES modules, which is the default style for new Node.js projects.
| Aspect | Callbacks | Promises with async/await |
|---|---|---|
| Sequential steps | Nested | Flat, top to bottom |
| Error handling | Check err at every step | One try/catch or .catch() |
| Parallel work | Manual counters | Promise.all() |
| Returning a value | Not possible, use the callback | return as usual |
| Stack traces | Short, per callback | Better, with async stack traces |
Callbacks are still valid for events (emitter.on('data', cb)) and for simple one-off operations. The problem is only deep nesting of dependent steps.
No. Callbacks passed to Array.prototype.map or forEach run synchronously. A callback is only asynchronous when the function that receives it defers the call, as fs.readFile or setTimeout do.
Node.js uses a single JavaScript thread and non-blocking I/O. Callbacks were the simplest way to say "run this when the operation finishes" before promises were part of the language.
Mostly in older code and in libraries that have not moved to promises. New code normally uses promise-based APIs with async/await, so deep nesting is rare.
No. Wrap legacy functions with util.promisify when you need them in async code, and leave event listeners as callbacks.
A callback is a function passed to another function to be called later. Nested dependent callbacks produce callback hell: deep indentation, repeated error checks and hard-to-test code. You can reduce it by naming and flattening functions, but the standard fix today is promise-based APIs combined with async/await, with util.promisify bridging old callback code.