加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在Angular中,承诺的错误和catch函数之间的概念差异

发布时间:2020-12-17 07:45:19 所属栏目:安全 来源:网络整理
导读:我终于得到了Angular promise错误处理,但对我来说是直觉的.我期望错误由失败回调处理,而是我不得不使用一个catch. 在概念上我不太明白为什么执行catch而不是失败回调. 我预期的 SomeAsyncService.getData().then(function (result) { // The call is success
我终于得到了Angular promise错误处理,但对我来说是直觉的.我期望错误由失败回调处理,而是我不得不使用一个catch.

在概念上我不太明白为什么执行catch而不是失败回调.

我预期的

SomeAsyncService.getData().then(function (result) {
    // The call is successful.
    // Code in this block throws an error.
},function (error) {
    // I expected to handle errors here.
});

最后的工作

SomeAsyncService.getData().then(function (result) {
    // The call is successful.
    // Code in this block throws an error.
}).catch(function (error) {
    // Where the error is actually caught. 
});

如果有更合适的方式处理承诺错误,请告知我们.

在应用程序代码中,第二个参数应该几乎从不被使用,也可以使用第一个参数.它主要是关于不同实现之间的承诺图书馆互操作性.

你应该总是使用.catch,除非你特别有一些你需要的奇怪的角落.(succcess,fail).

见The .then(success,fail) anti-pattern.

另外Q库(一个角$q基于)has similar section in their readme

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读