Callback
In short
A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
What is a callback?
A callback is a function you hand to another function so that it can be 'called back' at the right moment. The receiving function decides when to run it: immediately, once some work is done, or every time an event occurs. This is possible in languages like JavaScript because functions can be passed around like any other value.
Leaving your phone number at a busy restaurant is a good analogy for a callback. Instead of waiting at the door, you go about your day, and when your table is ready, they call you back. In code, callbacks are used the same way: to respond to button clicks, to handle the result of reading a file, or to customize behavior, as in array.map(callback).
Callbacks can be synchronous or asynchronous. A synchronous callback runs right away, during the call, as with forEach or map. An asynchronous callback runs later, after something like a timer, a network request, or a file read completes.
Relying on many nested asynchronous callbacks can lead to deeply indented, hard-to-read code known as 'callback hell'. Promises and async/await were added to JavaScript largely to solve this, but callbacks are still everywhere, especially in event listeners and array methods.
At a glance
Key takeaways
- A callback is a function passed into another function to be run later.
- The receiving function controls when and how often the callback runs.
- Callbacks can be synchronous (like
map) or asynchronous (likesetTimeout). - Deeply nested async callbacks cause 'callback hell', which promises and async/await help avoid.
Example
// Synchronous callback: runs once for each item, right away
const prices = [10, 20, 30];
const withTax = prices.map((price) => price * 1.2);
// Asynchronous callback: runs later, after 1 second
setTimeout(() => {
console.log("One second has passed");
}, 1000);
// Event callback: runs every time the button is clicked
button.addEventListener("click", () => console.log("Clicked!"));Readers ask
What is the difference between a callback and a promise?
A callback is a function you pass in to be called when work finishes. A promise is an object that represents the future result of that work, which you can chain with .then() or wait for with await; promises make sequencing and error handling much simpler than nested callbacks.
What is callback hell?
Callback hell is code where many asynchronous callbacks are nested inside each other, forming a pyramid shape that is hard to read, debug, and handle errors in. It is usually fixed by switching to promises or async/await.
Are callbacks always asynchronous?
No. Callbacks passed to array methods like map, filter, and forEach run synchronously, immediately during the call. Only callbacks tied to timers, events, or input/output operations run asynchronously.
Often compared
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- PromiseProgramming Fundamentals, p. 46A promise is an object that represents the eventual result of an asynchronous operation, letting code react to success or failure once the work completes.
- Async/AwaitProgramming Fundamentals, p. 4Async/await is a syntax for writing asynchronous code that reads like ordinary step-by-step code, pausing at each await until a promise or task finishes.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
Spotted a mistake or something missing on this page?Suggest an edit