0
0
Node.jsframework~5 mins

Callback pattern and callback hell in Node.js

Choose your learning style9 modes available
Introduction

Callbacks let you run code after something else finishes, like waiting for a friend before starting a game. But too many callbacks inside each other can make code hard to read and fix.

When you want to do something after a file finishes loading.
When you need to wait for a database to respond before continuing.
When you want to run a function after a timer ends.
When you handle user actions that happen one after another.
When you call APIs that respond asynchronously.
Syntax
Node.js
function doSomething(data, callback) {
  // do work
  callback(result);
}

doSomething(input, function(result) {
  // handle result
});
Callbacks are functions passed as arguments to run later.
The callback runs after the main task finishes.
Examples
This example shows a simple callback that prints a greeting after the function runs.
Node.js
function greet(name, callback) {
  callback(`Hello, ${name}!`);
}

greet('Alice', function(message) {
  console.log(message);
});
Here, a callback handles success or error after reading a file.
Node.js
readFile('file.txt', function(err, data) {
  if (err) {
    console.error('Error:', err);
  } else {
    console.log('File data:', data);
  }
});
This shows nested callbacks, which can get hard to read. This is called callback hell.
Node.js
doStep1(function(result1) {
  doStep2(result1, function(result2) {
    doStep3(result2, function(result3) {
      console.log('Final:', result3);
    });
  });
});
Sample Program

This program runs three steps one after another using callbacks. Each step waits half a second, then calls the next. The nested callbacks show how callback hell looks.

Node.js
function step1(callback) {
  setTimeout(() => {
    console.log('Step 1 done');
    callback('data from step 1');
  }, 500);
}

function step2(data, callback) {
  setTimeout(() => {
    console.log('Step 2 done with', data);
    callback('data from step 2');
  }, 500);
}

function step3(data, callback) {
  setTimeout(() => {
    console.log('Step 3 done with', data);
    callback('all steps done');
  }, 500);
}

step1(function(result1) {
  step2(result1, function(result2) {
    step3(result2, function(finalResult) {
      console.log(finalResult);
    });
  });
});
OutputSuccess
Important Notes

Too many nested callbacks make code hard to read and maintain.

Use named functions or promises to avoid callback hell.

Always handle errors in callbacks to avoid silent failures.

Summary

Callbacks run code after an action finishes.

Nested callbacks cause callback hell, making code messy.

Better patterns exist to keep code clean and easy to follow.