Reputation: 16981
So according to the documentation (and practical experience ;)) AngularJS's $q
"then" method returns new promise (https://docs.angularjs.org/api/ng/service/$q "The Promise API" section).
Consider piece of code like this:
// method somewhere in MyService service
// ...
loadItem = function() {
return OtherService.load().then(function(item) {
item.preprocessed = true;
return item;
});
}
// end of loadItem method
// Somewhere along the controller:
MyService.loadItem().then(function(item) {
// do something with an item...
alert(item.preprocessed);
}, function(error) {
alert('Error!');
})
Now, I want to do some processing on promise return value, but at the same time I want rejections to be pushed through the promises chain without me having to manually reject the promise on every step.
In example above if the item is loaded ok, the preprocessed property is set to true, and correct handler on controller then()
method is fired, but when something goes wrong and result of OtherService.load() gets rejected, the rejection handler in the controller code won't be fired.
Is there a way to overcome this, maybe there's a syntax that allows rejections to pass through?
Upvotes: 0
Views: 233
Reputation: 42669
Since you are returning the promise returned as part of invocation of then
in the error callback do
return OtherService.load().then(function(item) {
item.preprocessed = true;
return item;
},
function(error) {
return $q.reject(error);
});
And the error callback would fire on the controller.
Upvotes: 1