How to always run some code when a promise is fulfilled in Angular.js
The feature has been implemented in this pull request and is now part of AngularJS. It was initially called "always" and then later renamed to finally
, so the code should be as follow:
LoadingOverlay.start();
Auth.initialize().then(function() {
// Success handler
}, function() {
// Error handler
}).finally(function() {
// Always execute this on both error and success
});
Note that since finally
is a reserved keyword, it might be necessary to make it a string so that it doesn't break on certain browsers (such as IE and Android Browser):
$http.get('/foo')['finally'](doSomething);
I'm using Umbraco version 7.3.5 back end with AngularJS version 1.1.5 and found this thread. When I implemented the approved answer I got the error:
xxx(...).then(...).finally is not a function
What did work however was always
. If anyone else using an old version of AngularJS finds this thread and can't use finally
use this code instead
LoadingOverlay.start();
Auth.initialize().then(function() {
// Success handler
}, function() {
// Error handler
}).always(function() {
// Always execute this on both error and success
});