Callbacks

medium

Complete guide with examples and interview questions · 30 min

Definition

Say it out loud in an interview — clear, simple, no textbook wording.

If someone asks what a callback is, I'd say: it's a function you hand to another function, to be called later. "Call me back when you're done." Timers, network requests, event listeners — they all take callbacks.

The trap interviewers probe is assuming every callback is async. It's not — the callback doesn't decide. The caller does. forEach runs your callback right away; setTimeout parks it until the timer fires.

Senior rounds go one layer deeper: error handling (the error-first (err, result) convention) and trust (once your callback is inside someone else's code, they can call it never, twice, or synchronously). That's the real reason promises exist.

Quick Stats
30
Minutes
medium
Difficulty
5
Interview Q's

Key takeaways

  • A callback is a function you hand to another function to run later — it is not automatically async
  • Async callbacks (timers, network, events) run after the current code finishes
  • Error-first convention: the first callback argument is the error, the second is the result
  • Deep nesting becomes callback hell — fix it with named functions, promises, or async/await
  • Handing a callback to foreign code is a trust problem: it may fire never, twice, or synchronously

Track your mastery

Sign in to save Q&A scores, coding challenge progress, points, and streaks across devices.

Sign in to save progress