jQuery.Deferred "then" callback giving unexpected behavior
It seems as if the then()
callback is not firing when a Deferred
object is reject
ed. It was my understanding then()
should be executed whether the Deferred
resolves or rejects.
Here is an example (open in a jsFiddle console)
var doDfd = function(input){
var dfd = $.Deferred();
if(input == 'fail'){
dfd.reject();
}
else{
dfd.resolve();
}
return dfd.promise();
}
doDfd('fail')
.done(function(r){console.log('success')})
.fail(function(r){console.log('fa开发者_运维百科il')})
.then(function(r){console.log('then logic')});
deferred.then takes two arguments - one is a done callback, the other one is a fail callback (or callbacks because any of those arguments can be an array of functions).
See this fiddle: http://jsfiddle.net/Mrqf8/480/
精彩评论