1

We can handle response with .always() callback function in jQuery whether response is success or not.

Is there an equivalent of this type of usage in AngularJS?

//jQuery
$.get( "test.php" ).always(function() {
  alert( "$.get completed with success or error callback arguments" );
});

//AngularJS
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    //success
  }, function errorCallback(response) {
    //error
  });

//how can i handle always?
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
Okan Kocyigit
  • 12,913
  • 18
  • 65
  • 126
  • possible (but not exact) duplicate of [angular $http / jquery complete equivalent](http://stackoverflow.com/q/18144212/1048572)? – Bergi Feb 12 '16 at 15:45

2 Answers2

2

You can use the finally method of Angular promises.

Bergi
  • 572,313
  • 128
  • 898
  • 1,281
1

The finally() method from $q service used for this:

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    //success
}).catch(function errorCallback(response) {
    //error
}).finally(function() {
    //Callback method
    //Executed always, no matter of success or fail
});

An important notice when returning promises from finally() callback:

finally returns a promise, which will become resolved with the same fulfillment value or rejection reason as promise. However, if callback returns a promise, the resolution of the returned promise will be delayed until the promise returned from callback is finished.

Dmitri Pavlutin
  • 16,530
  • 7
  • 35
  • 38