Reputation: 3272
I have a chain of promises, where I use catch to catch errors.
this.load()
.then(self.initialize)
.then(self.close)
.catch(function(error){
//error-handling
})
What function gets called if the chain has finished without rejection? I was using finally, but this gets called also if an error occurs. I'd like to call a function after the catch-function which gets only called if no promise was rejected.
I'm using node.js with the q - module.
Upvotes: 2
Views: 71
Reputation: 708036
I would change your .catch()
to be a .then()
and supply both an onFullfill and an onRejected handler. Then you can determine exactly which one happened and your code is very clear that one or the other will execute.
this.load()
.then(self.initialize)
.then(self.close)
.then(function() {
// success handling
}, function(error){
//error-handling
});
FYI, this isn't the only way to do things. You could also use .then(fn1).catch(fn2)
which would similarly call fn1 or fn2 based on what the promise state was prior except that both could get called if fn1 returned a rejected promise or threw an exception because that would also be handled by fn2.
Upvotes: 3
Reputation: 4951
Adding another then will do.
this.load()
.then(self.initialize)
.then(self.close)
.then(function() {
//Will be called if nothing is rejected
//for sending response or so
})
.catch(function(error){
//error-handling
})
Upvotes: 5