DELAPARRA
Index
Color
NL
Category: Programming paradigms

Callback

Explained by Updated

What is Callback?

Callback: A function passed to another function to be called when an event completes. The classic pattern before promises and async/await. A callback is a function you pass to another function, to be called later. Often it runs when something finishes, such as a file read, a timer or a click.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Callback

How is Callback used in practice?

In JavaScript, button.addEventListener('click', handleClick) passes handleClick as a callback. In PHP, array_map(fn ($x) => $x * 2, $numbers) passes a callback that is applied to every item.

What should you keep in mind with Callback?

For asynchronous work, callbacks nested inside callbacks quickly become hard to read and hard to handle errors in, which earned the name "callback hell". Promises and async/await solved most of that in JavaScript. Callbacks are still everywhere for events, sorting, filtering and hooks. Keep them short, and give them a name when they do more than one line of work.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with C

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.