O'Reilly logo

Async JavaScript by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Passing Data to Callbacks

A Promise can give its callbacks additional information. For example, these two Ajax snippets are equivalent:

 
// Using a callback directly
 
$.get(url, successCallback);
 
// Binding a callback to a Promise
 
var​ fetchingData = $.get(url);
 
fetchingData.done(successCallback);

When you resolve or reject a Deferred, any arguments you provide are relayed to the corresponding callbacks.

 
var​ aDreamDeferred = ​new​ $.Deferred();
 
aDreamDeferred.done(​function​(subject) {
 
console.log(​'I had the most wonderful dream about'​, subject);
 
});
 
aDreamDeferred.resolve(​'the JS event model'​);
<= 
I had the most wonderful dream about the JS event model

There are also special methods for running the callbacks in a particular ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required