DEV Community

Shashi Bhushan Kumar
Shashi Bhushan Kumar

Posted on

How Promises Fix Callback Hell.?

In the previous post, we saw how Callback Hell makes code confusing
when callbacks are nested again and again.

Now letโ€™s see how Promises fix this problem.

Real-life example (Chai Shop continued โ˜•)

Earlier, the chai wala was saying:

  • Iโ€™ll make tea after sugar is added
    • Sugar will be added after milk comes
    • Milk will come after gas is on

Everyone was confused ๐Ÿ˜ตโ€๐Ÿ’ซ

Now imagine the chai wala says this instead:

โ€œI promise Iโ€™ll make the tea properly.

If everything goes well, youโ€™ll get your tea.

If something goes wrong, Iโ€™ll tell you.โ€

Suddenly, things feel clear and organized.

Thatโ€™s exactly how Promises work.

Promise way of doing the same work

makeTea()
  .then(addSugar)
  .then(addMilk)
  .then(boilTea)
  .then(serveTea)
  .catch(handleError);
Enter fullscreen mode Exit fullscreen mode

Why Promises are better than callbacks

  • Code is clean and readable
  • Easy to understand the flow
  • Errors can be handled in one place
  • Easy to add or remove steps

Simple comparison

  • Callbacks โ†’ messy when steps increase
  • Promises โ†’ clean and manageable

One-line summary
Promises turn confusing nested callbacks into a clear step-by-step flow.

Next Question for you.๐Ÿ‘‡

๐Ÿ‘‰ How async/await makes Promises even easier

Top comments (0)