Reputation: 5491
I have the sample code below which uses jQuery deferred. What I can't seem to understand is although the brushTeeth function returns a promise that is rejected, why is collectionResults which is another deferred always being resolved.
Some jQuery deferred reading says if the functions passed in $.when are not promises, they will be resolved right away, but brushTeeth actually returns a promise.
Clues what I am doing wrong here?
ShowerModule = ( function($) {
function init(){
var result = $.Deferred();
var collectionResults = $.when(brushTeeth);
collectionResults.done(function(){
console.log("done");
})
collectionResults.fail(function(){
console.log("reject");
})
}
function brushTeeth() {
var result = $.Deferred();
result.reject('["bah"]');
return result.promise();
}
return {
init : init
}
}(jQuery));
ShowerModule.init();
Upvotes: 1
Views: 133
Reputation: 36551
you have created two deffered object with the samename result
so i guess $.when is taking the one above it as promise.. is which is not rejected.. try this
function init(){
// var result = $.Deferred(); remove this
var collectionResults = $.when(brushTeeth()); //accept function and not the function's reference
collectionResults.done(function(){
console.log("done");
})
collectionResults.fail(function(){
console.log("reject");
})
}
Upvotes: 0
Reputation: 5491
Figured it out
var collectionResults = $.when(brushTeeth);
Line above should have been
var collectionResults = $.when(brushTeeth());
Upvotes: 1