Callbacks
As you explored in Chapter 10, Accessing External Resources, callbacks are the oldest and simplest means of executing asynchronous functionality in JavaScript. A callback is a specified function to be called once the result of an operation is ready. You saw this with the jQuery $.ajax()
and $.getJSON()
methods, where a function is called once a successful service call response is available; for example:
$.getJSON('https:/www.somesite.com/someservice', function(data) { // this function is a callback and is called once // the response to the service call is received } );
Another area where callbacks are heavily used is for event handlers. Events can be considered asynchronous, as they can happen at unpredictable times and in any order. The callbacks to...