Promises : Go To Next Error Function
Solution 1:
I tough a return inside the error function would automatically call the next error function.
No, returning means that you recovered from error situation and next will be resolved success callback. If you return from error callback and you want to pass it to the next one in chain, you need to return rejected promise:
dataService.saveRequest()
.then(functionresult(res) {
//Logged when the service sends back a resultconsole.log("finished");
}, functionerror(error) {
console.log("error from controller");
return $q.reject('error from controller');
});
Or instead or returning you can throw
something.
Solution 2:
When you return a value, rather than a Promise, from a handler, it is implicitly wrapped with Promise.resolve
. That applies to rejection handlers too, so your rejection handler is returning a resolved promise.
You need to throw
or return a rejected Promise to propagate rejection:
return$http.post('rest/request/send-request', someData)
.then(function(result){
//Goes, as expected, into the success function in the controllerreturn result.data;
}, function(error){
throwerror;
});
Solution 3:
You have to return return $q.reject();
from a promise in order for the next promise in the chain to fail too. See example plunker: http://plnkr.co/edit/porOG8qVg2GkeddzVHu3?p=preview
The reason is: Your error handler may take action to correct the error. In your error-function your dealing with the error,if not specified otherwise, it will return a new promise which is resolved. Therefore it is not reasonable to have the next promise failing by default. Think of it as try/catch.
You're catching the error and handling it - so it gets to the success handler. If you want to reject it, you have to do it by returning $q.reject();
Post a Comment for "Promises : Go To Next Error Function"