JQuery中Deferred.then(null,func)和Deferred.fail(func)之间的差异? [英] Differences between Deferred.then(null,func) and Deferred.fail(func) in JQuery?

查看:68
本文介绍了JQuery中Deferred.then(null,func)和Deferred.fail(func)之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天发现 Deferred.then(null,func) Deferred.fail(func) aren在JQuery中也是如此。在ES6的承诺中, Promise.then(null,func) Promise.catch(func)是一回事,所以我对JQuery的功能感到困惑。

I found out today that Deferred.then(null,func) and Deferred.fail(func) aren't the same thing in JQuery. In ES6's promise, Promise.then(null,func) and Promise.catch(func) are the same thing, so I was confused by JQuery's functions.

我所知道的唯一区别是:

The only difference I know of is this:

$.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
  });

还有其他有用的差异吗?

Are there any other useful differences?

推荐答案

是的,它们之间的区别在于 .fail()确实会返回它在<$ c $时调用的原始承诺c> .then()确实构造了一个可能用不同值解析的新承诺。

Yes, the difference between them is that .fail() does return the original promise it was called upon while .then() does construct a new promise which might resolve with a different value.

但是,由于 jQuery有问题的错误处理你不会注意到这一点,除非你从你的回调中返回一个承诺,就像你在你的例子中那样。如果您将 fail 与标准附加的 catch 调用进行比较,则会出现更多问题。

However, due to jQuery's problematic error handling you won't notice this unless you return a promise from your callback, like you do in your example. It would be much more of a problem if you had compared fail to a standard-adhering catch invocation.

这篇关于JQuery中Deferred.then(null,func)和Deferred.fail(func)之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆