Reputation: 7971
What does .deferred()
do? What is its work, and when and where should we use it with resolve
?
In the below function, I have two .done()
callbacks. I want to set two different callback messages.
fiddle
var deferred = $.Deferred();
deferred.done(function(value) {
alert(value);
}).done(function(id){
alert(id)
});
console.log(deferred)
deferred.resolve("hello world");
Upvotes: 5
Views: 15006
Reputation: 51800
jQuery.Deferred
is the main tool of jQuery's implementation of the promise pattern.
Here are a few links, worth reading :
As for your specific need : you should use .then()
(read the docs for .done()
and .then()
).
var deferred = $.Deferred();
deferred.then(function(value) {
alert(value);
return 42;
}).then(function(id){
alert('The answer : ' + id);
});
console.log(deferred)
deferred.resolve("hello world");
Upvotes: 5
Reputation: 44376
Would this do what you want?
var deferred = $.Deferred();
deferred.done(function(value) {
alert(value[0]);
}).done(function(value){
alert(value[1]);
});
deferred.resolve(["hello world", "goodbye, cruel world"]);
Upvotes: 2