A callback function is a function you pass as an argument to another function so that the other function can call it later. The receiving function decides when to run it, for example after each array item, after a timer, or when a file has been read. A javascript callback function is the basic building block behind array methods, event handlers and the original style of asynchronous code.
This guide covers synchronous and asynchronous callbacks, error handling conventions, and why promises replaced deeply nested callbacks.
In JavaScript, functions are first-class values. You can store them in variables, return them, and hand them to other functions. That is what makes callbacks possible.
greet does not know which formatting will happen. It only knows to call the function it was given. Pass shout, not shout(): the parentheses would call it immediately and pass its result instead.
Many built-in methods accept a callback and run it straight away for each element. Nothing is delayed.
The callback receives the element, its index and the array, in that order. You can write your own higher-order function in the same way:
An asynchronous callback is called later, after the current code has finished. JavaScript runs one piece of code at a time, and the event loop picks queued callbacks when the call stack is empty.
Even with a delay of 0, the callback waits until the running code ends. The same applies to DOM events:
Here the browser stores your function and calls it each time the user clicks.
A callback cannot be wrapped in a try...catch that surrounds the call which started the async work, because the error is thrown on a later turn of the event loop. Older Node.js APIs solved this with the error-first convention: the first argument is an error or null, and the result follows.
Always check err first and return early. Forgetting to do so is a common source of confusing crashes. Also make sure a callback is not called twice, or sometimes synchronously and sometimes asynchronously, because that makes behaviour hard to predict.
When each step depends on the previous one, callbacks nest deeper and deeper, which is called callback hell or the pyramid of doom.
Promises and async/await flatten this, give one place for error handling, and are what modern APIs such as fetch and node:fs/promises return.
Callbacks are still the right tool for events, array methods and simple hooks. For a single future result, prefer a promise. You can wrap an old callback API with util.promisify in Node.js.
No. Callbacks passed to map, filter, forEach and sort run synchronously. Only callbacks given to timers, events, I/O and similar APIs run later.
A callback is the function being passed in. A higher-order function is the function that receives a function as an argument or returns one.
this?A regular function used as a callback gets this from how it is called, not where it was written. Use an arrow function or bind to keep the this you expect.
Yes for events and array methods. For asynchronous results that happen once, use promises with async and await.
A callback is a function passed to another function to be called later. Synchronous callbacks power array methods, while asynchronous callbacks power timers, events and older I/O APIs. Pass the function itself without calling it, handle errors explicitly, and move to promises when callbacks start nesting.