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

Get Async JavaScript 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.