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

angularjs – Angular HttpPromise:`success` /`error`方法和`t

发布时间:2020-12-17 09:23:14 所属栏目:安全 来源:网络整理
导读:根据 AngularJS doc,调用$ http返回如下: Returns a promise object with the standard then method and two http specific methods: success and error . The then method takes two arguments a success and an error callback which will be called wit
根据 AngularJS doc,调用$ http返回如下:

Returns a promise object with the standard then method and two http specific methods: success and error. The then method takes two arguments a success and an error callback which will be called with a response object. The success and error methods take a single argument – a function that will be called when the request succeeds or fails respectively. The arguments passed into these functions are destructured representation of the response object passed into the then method.

除了响应对象在一种情况下被破坏的事实,我没有得到之间的区别

>传递成功/错误回调作为promise.then的参数传递
>作为promise的promise.success / promise.error方法的参数传递的回调

有没有?这两种不同的方式传递看似相同的回调的点是什么?

NB这个答案在事实上不正确;如下面的注释所指出的,success()返回原始的promise。我不会改变;并将其保留到OP进行编辑。

2的主要区别是.then()调用返回一个promise(使用从回调返回的值解析),而.success()是更传统的注册回调方法,不返回promise。

基于Promise的回调(.then())使得链接promises变得容易(执行调用,解释结果,然后做另一个调用,解释结果,做另一个调用等)。

.success()方法是一个简化的,方便的方法,当您不需要链调用或使用promise API(例如,在路由)。

简而言之:

> .then() – promise API的全部功能,但稍微更冗长> .success() – 不返回一个promise,但offeres略多一些convienient语法

(编辑:李大同)

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

    推荐文章
      热点阅读