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

Bitwise Operations Part 1: A Quick Introduction to Bases

In this series, I will touch on the fundamentals of understanding binary and learning to reason with it, then how we can use it in Javascript. What is decimal notation, really? We learn to count in decimals, or base 10. What this means... Continue →