You're using the slow version of egghead. Click here to try the faster version! ⚡️

Composing Closures and Callbacks in JavaScript

ClockIcon4h 20m
javascriptJavaScript
John Lindquist

John Lindquist

John Lindquist is a co-founder of egghead.io John has spent the majority of his career developing Rich Web Applications. Before starting egghead.io, John worked a...

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 what 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 broadcasting values to another function that is listening for them.

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.

In this course, John Lindquist guides you from a blank JavaScript file all the way through creating a library of reusable functions not unlike those found in RxJS. The difference is you'll be writing from scratch.

You'll learn techniques for solving Callback Hell with composition, implementing debouncing, and building a word game among several other examples.

Using callbacks in code is a major pattern in JavaScript, and learning to work with them effectively will take your problem-solving skills to the next level.

What you'll learn

  • CheckIconClosures, Callbacks, and Composition
  • CheckIconCurrying, Caching, and Creating operators
  • CheckIconCreating custom React Hooks
John Lindquist

John Lindquist

John Lindquist is a co-founder of egghead.io John has spent the majority of his career developing Rich Web Applications. Before starting egghead.io, John worked a...

Community Notes

Read helpful summaries and further explanations of this course, made by learners just like you.

View Notes