How to Depreciate .success() and .error() in Angular 1.5

So Angular is depreciating .success() and .error() in $http. For anyone using the two methods, there’s two pieces of good news!

 1. Updating the calls is dead simple.

.then() takes a success function and an error function. You can just refactor your code from:

$http.get(...)
  .success(function(data, ...) {
    ...
  })
  .error(function(data, ...) {
    ...
  });

To:

$http.get(...).then(
  function success(response) {
    var data = response.data;
    ...
  },
  function error(response) {
    var data = response.data;
  });

 2. You can still use it (..for now)

If refactoring isn’t that simple for you, you’re still in luck! You can still enable .success() and .error() through $httpProvider. It defaults to true.. for now.

 Why The Depreciation?

.success() and .error() returns the original promise, as opposed to .then(), which returns a new promise. The two methods in question returns the original promise because of the nature their API design. In order to write this:

$http.get(...)
  .success(function(data, ...) {
    ...
  })
  .error(function(data, ...) {
    ...
  });

One must return the original promise. This leads to awkward promise chaining with the success-error paradigm. You could chain successes together, but the awkwardness comes from being unable to attach an .error() to the chain. Not to mention you’d probably use .then() inside the .success() function anyways. So that’s why in 1.5, Angular will do away with .success() and .error().

 
17
Kudos
 
17
Kudos

Now read this

How to Efficiently Merge Two Lists Unix Timestamps

Say you’re retrieving a comprehensive list of someone’s Github activities, and you now have data of each week’s additions, deletions, and commits. Great! We can just chart it out, nice and easy. Now, say someone comes in and asks to... Continue →