I am having trouble understanding the basic concept of error handling with a chain of promises. To learn the rules, I wrote a simple example, guessing what the result would be. But, unfortunately, he does not behave the way I would. I read several articles about the subject, but maybe I canβt get the details because of my poor English.
Anyway, here is my code:
var promiseStart = $q.when("start"); var promise1 = promiseStart.then(function() { return Serviceforpromise1.get(); }); var promise2 = promise1.then(function(data1) { return Serviceforpromise2.get(data1); },function(error) { return $q.reject(); }); var promiseend = promise2.then(function(data2) { return data2; },function(error) { return error; }); return promiseend;
Well, I know that it might be better encoded, but it's just for that purpose. Here is the code for the Serviceforpromise1 function:
function Serviceforpromise1() { ... return $http.get(*whatever*).then(function (data){ return data; },function(error) { return $q.reject(); }); }
Consider only the case of Serviceforpromise1 failure. A $ q.rejec t is sent back to the main chain, so I'm waiting for the " prom1. Then ( ") error callback, and it worked as expected. For example, I decided to pass the error to " prom2.then ", so in this error callback I added the line return $ q.reject () ; But it never reached the second error callback (" prom2.then "), and I don't understand why (for example, Serviceforpromise1, I returned the rejected promise!)
I will be happy to deeply understand what is happening here. Thank you for your help.
source share