Update:
To help future viewers of this post, I created this demo of pluma's answer.
Question:
My goal seems fairly straightforward.
step(1)
.then(function() {
return step(2);
}, function() {
stepError(1);
return $q.reject();
})
.then(function() {
}, function() {
stepError(2);
});
function step(n) {
var deferred = $q.defer();
//fail on step 1
(n === 1) ? deferred.reject() : deferred.resolve();
return deferred.promise;
}
function stepError(n) {
console.log(n);
}
The problem here is that if I fail on step 1, both stepError(1)
AND stepError(2)
are fired. If I don't return $q.reject
then stepError(2)
won't be fired, but step(2)
will, which I understand. I've accomplished everything except what I'm trying to do.
How do I write promises so that I can call a function on rejection, without calling all of the functions in the error chain? Or is there another way to accomplish this?
Here's a live demo so you've got something work with.
Update:
I kind of have solved it. Here, I am catching the error at the end of the chain and passing the data to reject(data)
so that I will know what issue to handle in the error function. This actually doesn't meet my requirements because I don't want to depend on the data. It would be lame, but in my case it would be cleaner to pass an error callback to the function rather than to depend on the returned data to determine what to do.
step(1)
.then(function() {
return step(2);
})
.then(function() {
return step(3);
})
.then(false,
function(x) {
stepError(x);
}
);
function step(n) {
console.log('Step '+n);
var deferred = $q.defer();
(n === 1) ? deferred.reject(n) : deferred.resolve(n);
return deferred.promise;
}
function stepError(n) {
console.log('Error '+n);
}
What you need is a repeating
.then()
chain with a special case to start and a special case to finish.The knack is to get the step number of the failure case to ripple through to a final error handler.
step(1)
unconditionally..then()
with the following callbacks:.then()
with no success handler and a final error handler.You can write the whole thing out longhand but it's easier to demonstrate the pattern with named, generalised functions :
see demo
Note how in
step()
, the deferred is rejected or resolved withn
, thus making that value available to the callbacks in the next.then()
in the chain. OncestepError
is called, the error is repeatedly rethrown until it is handled byfinalError
.Bit late to the party but this simple solution worked for me:
This allows you to break out of the chain.
Found
Promise.prototype.catch()
examples on MDN below very helpful.(The accepted answer mentions
then(null, onErrorHandler)
which is basically the same ascatch(onErrorHandler)
.)