Node.js Course
Node.js
/
Beginner

Callbacks

Definition

A function passed as an argument to another function, which is executed later once an asynchronous operation completes.

Explain Like I'm New

Ordering a pizza. You don't stand at the door waiting for 30 minutes (blocking). You give the pizza shop your phone number (the callback function). You go watch TV. When the pizza is ready, they ring your phone (execute the callback).

Real World Example

Using `setTimeout(callback, 1000)` or querying a database `db.findUser(id, function(result) { ... })`.

Common Use Cases

  • •Handling network requests
  • •File system I/O
  • •Timers

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Are all callbacks asynchronous?

intermediate

  • Why do callbacks often lead to poorly structured code?

Flash Cards

Question

Are all callbacks async?

Click to reveal answer
Answer

No! Functions like Array `.map()` or `.forEach()` accept callbacks, but execute them immediately and Synchronously. Only callbacks passed to Node APIs like `fs.readFile` or `setTimeout` are executed Asynchronously via the Event Loop.