After Angular.js $http request, call complete function regardless of success or failure of promise

前端 未结 3 1313
礼貌的吻别
礼貌的吻别 2021-02-13 18:46

How can I ensure that the complete() function will run regardless of the outcome of the $http call using the promise API provided with Angular.js?

$         


        
3条回答
  •  别跟我提以往
    2021-02-13 19:47

    I'm not the world's greatest expert in Angular.js but understand you can do as follows :

    whatever.then(function() {
        // success code here
    }, function() {
        // error code here
        return true; // return anything that's not undefined (and not a `throw()`) to force the chain down the success path at the following then().
    }).then(function() {
        // "complete" code here
    });
    

    You are essentially forced to contrive something from one or more .then(), which is a $q promise's only method.

提交回复
热议问题