Callback
A callback is a function passed into other code so it can be called later, often after an event, task, or asynchronous operation completes.
Callbacks are common in programming whenever one part of a system needs to tell another part what to do once something finishes or happens.
What it does
Callbacks let code defer behavior until the right moment.
It is commonly used to:
- Handle asynchronous results
- Respond to user events
- Customize behavior inside reusable functions
- Trigger follow-up logic after a task completes
- Decouple one part of a system from another
Core concepts
Function passed as data
A callback is usually a function that is passed into another function, method, or API.
The receiving code decides when to call it.
Deferred execution
The defining feature is not what the callback does, but when it runs.
It runs later, when some condition, event, or completion point is reached.
Common in async and event-driven code
Callbacks are especially common in asynchronous programming and event-driven systems.
They are one of the core ways code responds to operations that do not complete immediately.
Common use cases
- Event handlers in UI code
- Async API request handling
- Timers and scheduled work
- Iteration helpers
- Library hooks and extension points
Practical notes
- Callbacks are simple and powerful, but deeply nested callback chains can become hard to read.
- Modern languages and frameworks often provide alternatives such as promises, async/await, or higher-level abstractions.
- A callback is still a fundamental concept even when a codebase uses newer async patterns.
- The same codebase may use callbacks in both low-level APIs and high-level event systems.
Sources Used
Frequently Asked Questions
Is a callback the same as a normal function?
It is still a function, but it becomes a callback when it is passed somewhere else to be called later.
Are callbacks only for asynchronous code?
No. They are common there, but callbacks can also be used in synchronous APIs and reusable abstractions.
Are callbacks outdated?
No. They are still a core programming concept even when newer async patterns are preferred in some contexts.