Reputation: 36659
I have a service that returns a standard resolve or reject promise. In my controller, I currently have something like:
myService.myServiceFunction(id).then(function(data){
// do something with data
$scope.myServiceFunctionFinished = true;
}, function(data){
// do some error handling with data
$scope.myServiceFunctionFinished = true;
});
In the above, both the error and success responses will trigger $scope.myServiceFunctionFinished
being assigned a true
value.
Is there anyway to shorthand the above so I am not defining the same thing twice? I am looking for the equivalent of a finally block if the above code was a try catch statement. The variable assignment I showed above must happen after the server responds.
Upvotes: 2
Views: 283
Reputation: 136154
You could use .finally
function which will called once promise resolve/rejected
.
Code
myService.myServiceFunction(id).then(function(data){
//do success code
}, function(data){
//do error code
}).finally(function(){
//do some final stuff which wouldn't care about promise resolve/reject
$scope.myServiceFunctionFinished = true;
});
Upvotes: 6