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);
}
Promise.prototype.catch()
examples on MDN show solution for the exact same issues. – Embassy