Reputation: 384
I am just trying some demo code with JQuery deferred and I am not able to understand the behaviour.
This is my code:
( function(){
var createDef = function(data){
var def = new $.Deferred();
setTimeout(()=>{
alert(data);
def.resolve(data);
},0);
return def;
};
$
.when(createDef('hello'),createDef('world'))
.done((msg)=>{
alert('done ' + msg);
})
.fail((msg)=>{
alert('fail ' + msg);
})
.always((msg)=>{
alert('always ' + msg);
});
})();
in the line def.resolve(data);
in both cases def is in pending state but still only first message i.e. hello is triggered, why is second message not coming to done callback?
Upvotes: 0
Views: 35
Reputation: 2640
You need to add one more parameter to done() as shown below:
( function(){
var createDef = function(data){
var def = new $.Deferred();
setTimeout(()=>{
alert(data);
def.resolve(data);
},0);
return def;
};
$
.when(createDef('hello'),createDef('world'))
.done((msg, worldcallbackresult)=>{
//add another parameter to indicate createDef('world')
alert('done ' + msg + 'world callback' + worldcallbackresult);
})
.fail((msg)=>{
alert('fail ' + msg);
})
.always((msg)=>{
alert('always ' + msg);
});
})();
Upvotes: 1