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
  • 630,263
  • 148
  • 957
  • 1,375
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • 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
  • 630,263
  • 148
  • 957
  • 1,375
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
  • 18,122
  • 8
  • 37
  • 41