角HttpPromise:`success` /`error`方法和`then`的参数之间的区别 [英] Angular HttpPromise: difference between `success`/`error` methods and `then`'s arguments

查看:248
本文介绍了角HttpPromise:`success` /`error`方法和`then`的参数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 AngularJS文档,调用 $ HTTP 返回以下内容:

According to AngularJS doc, calls to $http return the following:

返回的对象与标准的然后方式和两个HTTP具体方法:成功错误即可。在然后方法有两个参数的成功错误回调将与响应对象调用。在成功错误方法需要一个参数 - 当一个请求成功或失败,分别将被调用的函数。传递到这些函数的参数解构再传递到方法,然后响应对象的presentation。

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.

除了事实响应对象在一种情况下解体,我没有得到

Aside from the fact that the response object is destructured in one case, I don't get the difference between


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

  • the success/error callbacks passed to be passed as arguments of promise.then
  • the callbacks passed as arguments for the promise.success/promise.error methods of the promise

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

Is there any? What's the point of these two different ways to pass seemingly identical callbacks?

推荐答案

NB 的这个答案是事实不正确;如下面的注释,成功(指出)不会返回原来的承诺我就不会改变。而让OP进行编辑。

NB This answer is factually incorrect; as pointed out by a comment below, success() does return the original promise. I'll not change; and leave it to OP to edit.

2之间的主要区别是,。然后()调用返回,而的承诺(从回调返回值来解析)。成功()是注册回调和不返回的承诺更传统的方式。

The major difference between the 2 is that .then() call returns a promise (resolved with a value returned from a callback) while .success() is more traditional way of registering callbacks and doesn't return a promise.

根据承诺,回调(。然后())可以很容易地链的承诺(做一个电话,除preT结果的然后做的另一个电话,除preT效果,在另一个电话等)。

Promise-based callbacks (.then()) make it easy to chain promises (do a call, interpret results and then do another call, interpret results, do yet another call etc.).

.success()方法是一个精简,方便的方法,当你不需要调用链,也不符合承诺API(举个例子,在路由)

The .success() method is a streamlined, convenience method when you don't need to chain call nor work with the promise API (for example, in routing).

在短:


  • 。然后() - 承诺API的全部功能,但稍微详细

  • .success() - 不返回的希望,但是offeres稍微convienient语法

  • .then() - full power of the promise API but slightly more verbose
  • .success() - doesn't return a promise but offeres slightly more convienient syntax

这篇关于角HttpPromise:`success` /`error`方法和`then`的参数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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