Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Is .finally() Called At the End of Every Path in a Chained Promise Decision Tree?

I have the following chained sequence of promises:

$scope.promisesInProgress = true
myService.myFirstPromise(id)
    .then(function(data){
        $scope.firstResponse = data;
        return myService.mySecondPromise(id);
    })
    .then(function(data){
        $scope.secondResponse = data;
    })
    .finally(function(){
        $scope.promisesInProgress = false;
    });

Is the finally() callback function being called at the very end no matter what the success / failure of the previous two promises are?

For example, if myFirstPromise() returned a 400 response, mySecondPromise() will never be called - but I assume the finally() block would still be thrown? The same should be true if mySecondPromise() returns a 400 (and $scope.secondResponse is never set) and if both promises return 200s.

like image 792
Lloyd Banks Avatar asked May 06 '16 15:05

Lloyd Banks


People also ask

What is promise method in Angular?

What Is Promise in Angular? Promises in Angular provide an easy way to execute asynchronous functions that use callbacks, while emitting and completing (resolving or rejecting) one value at a time. When using an Angular Promise, you are enabled to emit a single event from the API.

What is AngularJS promise?

Promises in AngularJS are provided by the built-in $q service. They provide a way to execute asynchronous functions in series by registering them with a promise object. {info} Promises have made their way into native JavaScript as part of the ES6 specification.

What is deferred and promise in AngularJS?

Simply put you can use $q. defer() to create a Promise. A Promise is a function that returns a single value or error in the future. So whenever you have some asynchronous process that should return a value or an error, you can use $q. defer() to create a new Promise.

What is .then in AngularJS?

then() function to handle the callbacks. Traditional promises (using the $q Service in Angular) have a . then() function to provide a continuation on success or failure, and . then() receives parameters for a success and failure callback.


1 Answers

Angular 1.x $q service inspired by Kris Kowal's Q, based on docs:

finally(callback, notifyCallback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.

so yes, no matter myFirstPromise resolved or rejected, the finally() block would always be called

UPDATED,

to be noticed, the finally() block of myFirstPromise would be called before mySecondPromise resolved(or rejected), because myFirstPromise and mySecondPromise are different promise instance, and mySecondPromise promise instance created after myFirstPromise resolved

like image 75
MarkoCen Avatar answered Nov 15 '22 03:11

MarkoCen