当Deferred
对象被reject
ed时,then()
回调似乎没有触发。我的理解是,无论 Deferred
解析还是拒绝,都应该执行 then()
。
这是一个示例 ( 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('fail')})
.then(function(r){console.log('then logic')});
最佳答案
deferred.then 接受两个参数 - 一个是完成回调,另一个是失败回调(或回调,因为这些参数中的任何一个都可以是函数数组)。
看到这个 fiddle :http://jsfiddle.net/Mrqf8/480/
关于javascript - jQuery.Deferred "then"回调给出意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5097421/