Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are Callbacks in Node.js and What Is Callback Hell?

Node.jsJavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 5, 2026
7 min read

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.

Table of Contents

  • What a callback is
  • Asynchronous callbacks in Node.js
  • How callback hell appears
  • Ways to avoid callback hell
    • Name your functions and keep them flat
    • Use promises with async/await
    • Convert old callback APIs with promisify
  • Callbacks versus promises at a glance
  • Frequently Asked Questions
    • Is a callback always asynchronous?
    • Why does Node.js use callbacks so much?
    • Is callback hell still a problem in 2026?
    • Do I have to rewrite all callbacks as promises?
  • Summary
  • Related Articles

What a callback is

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:

javascript
javascript
Loading syntax highlighting...
text

The callback passed to forEach is called synchronously, once per element. The callbacks that matter for Node.js are the ones called later.

Asynchronous callbacks in Node.js

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.

javascript
javascript
Loading syntax highlighting...
text

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.

How callback hell appears

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.

javascript
javascript
Loading syntax highlighting...

With three steps this is still readable. With ten steps, plus loops, retries and cleanup, the code forms a pyramid. The problems are practical:

  • Error handling is repeated at every level, and it is easy to forget one.
  • Variables from outer levels are visible everywhere, which makes names clash and logic hard to follow.
  • Running steps in parallel, or waiting for several, needs manual counters.
  • Stack traces are shallow and unhelpful, because each callback starts a fresh call stack.
  • Unit testing a single step is difficult when it lives inside another function.

Ways to avoid callback hell

Name your functions and keep them flat

The cheapest fix is to move each callback into a named function. Nothing is nested more than one level.

javascript
javascript
Loading syntax highlighting...

Each function can now be read and tested on its own. Early return statements after an error keep the happy path unindented.

Use promises with async/await

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.

javascript
javascript
Loading syntax highlighting...

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.

Convert old callback APIs with promisify

If a library still uses callbacks, util.promisify wraps any function that follows the error-first convention and returns a promise.

javascript
javascript
Loading syntax highlighting...

Top-level await works in ES modules, which is the default style for new Node.js projects.

Callbacks versus promises at a glance

AspectCallbacksPromises with async/await
Sequential stepsNestedFlat, top to bottom
Error handlingCheck err at every stepOne try/catch or .catch()
Parallel workManual countersPromise.all()
Returning a valueNot possible, use the callbackreturn as usual
Stack tracesShort, per callbackBetter, 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.

Frequently Asked Questions

Is a callback always asynchronous?

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.

Why does Node.js use callbacks so much?

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.

Is callback hell still a problem in 2026?

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.

Do I have to rewrite all callbacks as promises?

No. Wrap legacy functions with util.promisify when you need them in async code, and leave event listeners as callbacks.

Summary

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.

Related Articles

  • How Do Timers Like setTimeout and setInterval Work in Node.js?
  • What Is the Error-First Callback Pattern in Node.js?
  • Learn Node.js: Complete Study Roadmap from Beginner to Advanced
Share