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);
}
If I understand correctly, you want only the error for the failing step to show, right?
That should be as simple as changing the failure case of the first promise to this:
By returning
$q.reject()
in the first step's failure case, you're rejecting that promise, which causes the errorCallback to be called in the 2ndthen(...)
.The reason your code doesn't work as expected is that it's actually doing something different from what you think it does.
Let's say you have something like the following:
To better understand what's happening, let's pretend this is synchronous code with
try
/catch
blocks:The
onRejected
handler (the second argument ofthen
) is essentially an error correction mechanism (like acatch
block). If an error is thrown inhandleErrorOne
, it will be caught by the next catch block (catch(e2)
), and so on.This is obviously not what you intended.
Let's say we want the entire resolution chain to fail no matter what goes wrong:
Note: We can leave the
handleErrorOne
where it is, because it will only be invoked ifstepOne
rejects (it's the first function in the chain, so we know that if the chain is rejected at this point, it can only be because of that function's promise).The important change is that the error handlers for the other functions are not part of the main promise chain. Instead, each step has its own "sub-chain" with an
onRejected
that is only called if the step was rejected (but can not be reached by the main chain directly).The reason this works is that both
onFulfilled
andonRejected
are optional arguments to thethen
method. If a promise is fulfilled (i.e. resolved) and the nextthen
in the chain doesn't have anonFulfilled
handler, the chain will continue until there is one with such a handler.This means the following two lines are equivalent:
But the following line is not equivalent to the two above:
Angular's promise library
$q
is based on kriskowal'sQ
library (which has a richer API, but contains everything you can find in$q
). Q's API docs on GitHub could prove useful. Q implements the Promises/A+ spec, which goes into detail on howthen
and the promise resolution behaviour works exactly.EDIT:
Also keep in mind that if you want to break out of the chain in your error handler, it needs to return a rejected promise or throw an Error (which will be caught and wrapped in a rejected promise automatically). If you don't return a promise,
then
wraps the return value in a resolve promise for you.This means that if you don't return anything, you are effectively returning a resolved promise for the value
undefined
.http://jsbin.com/EpaZIsIp/20/edit
Or automated for any number of steps:
http://jsbin.com/EpaZIsIp/21/edit
When rejecting you should pass an rejection error, then wrap step error handlers in a function that checks whether the rejection should be processed or "rethrown" until the end of the chain :
What you'd see on the console :
Here is some working code https://jsfiddle.net/8hzg5s7m/3/
If you have specific handling for each step, your wrapper could be something like:
then your chain
Attach error handlers as separate chain elements directly to the execution of the steps:
or using
catch()
:Note: This is basically the same pattern as pluma suggests in his answer but using the OP's naming.
The best solution is to refactor to your promise chain to use ES6 await's. Then you can just return from the function to skip the rest of the behavior.
I have been hitting my head against this pattern for over a year and using await's is heaven.