DEV Community

Cover image for What is Callback Hell, and why is it considered a problem in JavaScript?
Shashi Bhushan Kumar
Shashi Bhushan Kumar

Posted on

What is Callback Hell, and why is it considered a problem in JavaScript?

Callback Hell (With Simple Example ☕)

Callback Hell happens when many callbacks are written inside each other.
The code starts going right → right → right,

and your eyes start searching for “end kaha hai?” 😵‍💫

Real-life example (Chai Shop ☕)

Imagine you go to a chai shop.

You say to the chai wala:

“Make a cup of tea,

after that add sugar,

after that add milk,

after that boil it,

after that give me the tea.”

Simple request. Right? 😄

Now imagine he replies like this:

  • I’ll make tea after sugar is added
    • Sugar will be added after milk comes
    • Milk will come after gas is on
      • Gas will be on after kettle is ready

At this point,

you are standing there thinking:

Everything depends on the previous step.

One small delay, and everything is stuck.

This becomes confusing very quickly.

This confusion is called Callback Hell.

Same idea in JavaScript

makeTea(function () {
  addSugar(function () {
    addMilk(function () {
      boilTea(function () {
        serveTea();
      });
    });
  });
});
Enter fullscreen mode Exit fullscreen mode

Why Callback Hell is a problem

  • Code becomes messy
  • Hard to understand the flow
  • Hard to handle errors
  • Difficult to maintain when logic grows

How developers avoid Callback Hell

  • Use Promises
  • Use async/await

One-line summary

Callback Hell is when callbacks are nested so deeply that code becomes confusing and unreadable.

Next Question For You 👇

👉 “How Promises fix Callback Hell.”

Top comments (0)