Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS then() behaves differently than success()-error() [duplicate]

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?

like image 404
Behrooz Avatar asked Feb 15 '16 17:02

Behrooz


People also ask

What happened to the success and error methods in AngularJS?

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.

Why do developers get confused between success and error?

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".

Is the $Q module in AngularJS outdated?

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:


1 Answers

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);
});

Response Object vs Four Arguments

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

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

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


Update

The .success and .error methods have been deprecated and removed from AngularJS V1.6.

For more information, see

  • Why are angular $http success/error methods deprecated? Removed from v1.6?.
like image 194
georgeawg Avatar answered Oct 25 '22 23:10

georgeawg