← Back to Roadmap

Callback Functions

Easy

In plain terms

Callbacks are functions passed as arguments to be called later. Used for async operations (setTimeout, event handlers, Node callbacks). Can lead to callback hell with nested async logic.

What you need to know

  • Function as argument
  • Called when async completes
  • Callback hell with nesting

Try it yourself

Copy the code below and run it in your browser console or a code editor:

setTimeout(() => console.log('Done'), 1000);

fs.readFile('file.txt', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Callback hell
getUser(id, (user) => {
  getOrders(user.id, (orders) => {
    getDetails(orders[0], (details) => {
      // Nested...
    });
  });
});

Learn more

Dive deeper with these trusted resources: