As the success()
and error()
functions are deprecated in AngularJS, I am updating my codes, replacing them with then()
. Now according to my understanding, these two pieces of codes should behave identically:
$http
.get(/* some params */)
.success(function() {
// success cases here
})
.error(function() {
// error cases here
});
And
$http
.get(/* some params */)
.then(function() {
// success cases here
}, function() {
// error cases here
});
But in some cases I am getting different behavior. Can you please explain to me why would that happen, and more importantly, what would be the way to guarantee the identical behavior using then()
functions?
The deprecated .success and .error methods have been removed from AngularJS 1.6. Due to b54a39, $http 's deprecated custom callback methods - .success () and .error () - have been removed. You can use the standard .then () / .catch () promise methods instead, but note that the method signatures and return values are different.
This issue highlights a number of situations where developers get confused because they either expect .success and .error to work the same way as .then or vice versa. In a perfect world I would rather just ditch these $http specific "promises".
Most likely, its content is outdated. Especially if it's technical. By writing this I'm taking a risk of looking like an idiot who has failed to read the docs. So please be gentle. AngularJS uses a promise module called $q. It originates from this beast of a project. You use it like this for example:
The .success
and .error
methods ignore return values.
Consequently they are not suitable for chaining.
var httpPromise = $http
.get(/* some params */)
.success(function onSuccess(data, status, headers, config) {
var modifiedData = doModify(data);
//return value ignored
return modifiedData;
})
.error(function onError(data, status, headers, config) {
// error cases here
});
httpPromise.then(function onFullfilled(response) {
//chained data lost
//instead there is a response object
console.log(response.data); //original data
console.log(response.status); //original status
});
On the otherhand, the .then
and .catch
methods return a derived promise suitable for chaining from returned (or throw) values or from a new promise.
var derivedPromise = $http
.get(/* some params */)
.then(function onFulfilled(response) {
console.log(response.data); //original data
console.log(response.status); //original status
var modifiedData = doModify(response.data);
//return a value for chaining
return modifiedData;
})
.catch(function onRejected(response) {
// error cases here
});
derivedPromise.then(function onFullfilled(modifiedData) {
//data from chaining
console.log(modifiedData);
});
Also notice that the $http
service provides four arguments (data, status, headers, config)
when it invokes the function provided to the .success
and .error
methods.
The $q
service only provides one argument (response) to the functions provided to the .then
or .catch
methods. In the case of promises created by the $http
service the response object has these properties:1
Chaining promises
Because calling the
then
method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs.2
The .success
and .error
methods have been deprecated and removed from AngularJS V1.6.
For more information, see
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With