CHAPTER 5 Ensuring Correct Use of the Callback Pattern
WHAT’S IN THIS CHAPTER?
- Creating and testing code that uses callback functions
- Creating and testing callback functions
- Identifying and addressing problems that are commonly encountered when implementing the Callback Pattern
WROX.COM CODE DOWNLOADS FOR THIS CHAPTER
The wrox.com code downloads for this chapter are found at www.wrox.com/go/reliablejavascript
on the Download Code tab. The code is in the Chapter 5 download and individually named according to the filenames noted throughout this chapter.
A callback is a function provided as an argument to a second function, which the second function will execute at some time in the future. That future time may be before the second function has exited, in which case the callback is considered synchronous. Alternatively, the future time may be after the second function has exited, in which case the callback is considered asynchronous. While all the examples in this chapter will use synchronous callbacks, the techniques (and potential problems) are also applicable to asynchronous callbacks. Chapter 6 will introduce promises, which are used exclusively with asynchronous callbacks.
The Callback Pattern is an important one to master when crafting reliable JavaScript because it is heavily used, both in the language proper and in many third-party libraries, such as JQuery.
The sections that follow illustrate how to create reliable callback functions when interacting with ...
Get Reliable JavaScript: How to Code Safely in the World's Most Dangerous Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.