【发布时间】:2015-06-09 05:26:01
【问题描述】:
我今天发现 Deferred.then(null,func) 和 Deferred.fail(func) 在 JQuery 中不是一回事。在 ES6 的 promise 中,Promise.then(null,func) 和 Promise.catch(func) 是同一个东西,所以我被 JQuery 的函数弄糊涂了。
我知道的唯一区别是:
$.Deferred().reject().promise()
.fail(function(){
return $.Deferred().resolve().promise();
})
.then(function(){
console.log('fail caught error'); // NOT printed
});
$.Deferred().reject().promise()
.then(null,function(){
return $.Deferred().resolve().promise();
})
.then(function(){
console.log('then caught error'); //printed
});
还有其他有用的区别吗?
【问题讨论】:
-
最好改掉使用任何 jQuery 专有方法的习惯,例如
.fail()和.done()。他们根本不做基于标准的.then()所做的事情。我刚刚决定只使用.then()和 jQuery。然后,当 jQuery 最终真正获得更多基于标准的时候,我不需要做太多的改变,或者如果我将任何 jQuery 承诺转换为 Bluebird 承诺,我也不需要改变任何东西。
标签: javascript jquery promise