How to always run some code when a promise is fulfilled in Angular.js

前端 未结 4 653
囚心锁ツ
囚心锁ツ 2020-12-13 23:17

In my Angular.js application, I\'m running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I nee

4条回答
  •  天命终不由人
    2020-12-13 23:46

    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);
    

提交回复
热议问题