-
Notifications
You must be signed in to change notification settings - Fork 27.5k
$http .success/.error return original promise, dissimilar from how .then works. Clarify in docs? #10508
Comments
IMO |
I'm starting to feel the same way. At first I considered A) I was not bothering to attach Given those points, in the future I will personally be avoiding |
Yeh, I'm also quite in favour of using promise-based API over I don't think removing |
If anyone can think of good wording for the doc update I would be happy to merge. |
I'll have a go at adding a line to the docs and making a pull request (when I have a moment to go through the 25-point contribution checklist, that is). Expect to see the request this week. |
Just ran into this myself. Did a nice fiddle for it, but then I found this issue. I would really like to either remove the methods or fix them. Adding to docs feels like the easy way out. |
I think we should be deprecating $http.get(...)
.success(function(data, ...) {
...
})
.error(function(data, ...) {
...
}); to writing $http.get(...).then(
function success(response) {
var data = response.data;
...
},
function error(response) {
var data = response.data;
}); while it does require a deepish understanding of promise to understand why |
@petebacondarwin would marking them as depreciated a first step of removing them in 1.5? |
Exactly |
1. do not use success/error callback for $http cause they don't follow the promise standard 2. do not use defered API cause it's a duplicated promise referenced link: angular/angular.js#10508 http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern
The legacy methods, `success` and `error`, have been deprecated. Set this to `false` to cause `$http` to throw an error if these methods are used in the application. For now it defaults to `true`. In a future release we will remove these methods altogether. DEPRECATION NOTICE: The legacy methods 'success' and 'error' on promises returned by $http are now deprecated. Closes angular#12112 Closes angular#10508
The legacy methods, `success` and `error`, have been deprecated. Set this to `false` to cause `$http` to throw an error if these methods are used in the application. For now it defaults to `true`. In a future release we will remove these methods altogether. DEPRECATION NOTICE: The legacy methods 'success' and 'error' on promises returned by $http are now deprecated. Closes angular#12112 Closes angular#10508
@glebec Long time ago, I'm confused with success invocation too. It breaks the specification promise must be resolved exactly once. |
Hello.
$http
returns a normal promise for a server response object (withdata
,status
,headers
etc.). As a normal promise, returning values from a handler in.then
results in a new exported promise for the value (or assimilation, if the return value is a promise, or rejection in case of error, etc.). That is all well and good.For convenience/abstraction, AngularJS provides two custom methods on promises returned by
$http
:.success
and.error
. Each takes a single handler with thedata
,status
etc. as parameters.The problem is that people familiar with promises will likely try to chain off of
.success
, perhaps by transforming and returning new values or new promises. However,.success
does not return a new promise; rather, it returns the original promise:If you were trying to make
.success
chainable in the same way.then
is, it would be easy to change in the AngularJS source, and indeed I had started work on a pull request:However, I then realized that this would mean you could not attach both
.success
and.error
to a single promise. The reason you can write this code:…is because
.success
and.error
return the original promise, not a new promise representing the result of the previous method.This leaves me in a bit of a quandary. The recommendation must therefore be that
.success
and.error
are only suitable as "end of the road" promise consumers, when you do not intend to do any more post-processing / chaining. If that is the case, I think the AngularJS docs should reflect this, and make it explicitly clear that chaining off of.success
/.error
does not work in the normal promises fashion, since the original promise is returned.What do you think?
The text was updated successfully, but these errors were encountered: