I'm trying to figure out a good way to say "Do all of these things, but bail in the case that any of them fails"
What I have right now:
var defer = $q.defer();
this
.load( thingy ) // returns a promise
.then( this.doSomethingA.bind( this ) )
.then( this.doSomethingB.bind( this ) )
.then( this.doSomethingC.bind( this ) )
.then( this.doSomethingD.bind( this ) )
.then( function(){
defer.resolve( this );
} );
;
return defer.promise;
What I ultimately want is to somehow catch any error on that chain so I can pass it on to the defer
promise above. I don't particularly care if the syntax is kept similar to what I have above.
Or even if anyone can just tell me how to stop the above chain.
You can stop angularjs chain by returning rejected promise inside any then callback.
load()
.then(doA)
.then(doB)
.then(doC)
.then(doD);
where doA, doB, doC, doD can have logic like this:
var doA = function() {
if(shouldFail) {
return $q.reject();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With