As a developer, you’ll often make use of APIs that have you pass in a callback function. For example, adding event listeners or using various other DOM or Node APIs.
What’s less common is writing your own code that makes use of callbacks. Many developers know
a callback is, but not the why or how of implementing them in their projects.
A callback is a function that you pass to another function, so that the function that you pass it to controls the invocation of that function.
To help keep things straight, John Lindquist calls the innermost callback the Listener, and the function it gets passed into the Broadcaster.
Think of it as one function
values to another function that is
These Broadcaster-Listener callback relationships can be composed to solve problems in the realm of timing, buffering, and caching.
This pattern of composing callbacks is the foundation of RxJS and other libraries that handle complex async scenarios.
You’ll learn techniques for solving Callback Hell with composition, implementing debouncing, and building a word game among several other examples.