根据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.
除了response
对象在一种情况下被解构这一事实之外,我不明白两者之间的区别
- 作为
promise.then
的参数传递的成功/错误回调 - 作为 Promise 的
promise.success
/promise.error
方法参数传递的回调
有吗?这两种不同的方式来传递看似相同的回调有什么意义?
最佳答案
这里已经有一些很好的答案。但值得深入了解所提供的并行性的差异:
success()
返回原始 Promisethen()
返回一个新的 Promise
不同之处在于 then()
驱动顺序操作,因为每次调用都会返回一个新的 Promise。
$http.get(/*...*/).
then(function seqFunc1(response){/*...*/}).
then(function seqFunc2(response){/*...*/})
$http.get()
seqFunc1()
seqFunc2()
success()
驱动并行操作,因为处理程序链接在同一个 Promise 上。
$http(/*...*/).
success(function parFunc1(data){/*...*/}).
success(function parFunc2(data){/*...*/})
$http.get()
parFunc1()
、parFunc2()
并行
关于angularjs - Angular HttpPromise : difference between `success` /`error` methods and `then` 's arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16385278/