Upon referencing the documentation and personal experience, it is noted that AngularJS's $q
"then" method generates a new promise (https://docs.angularjs.org/api/ng/service/$q "The Promise API" section).
Let's consider the following code snippet:
// A method within the MyService service
// ...
loadItem = function() {
return OtherService.load().then(function(item) {
item.preprocessed = true;
return item;
});
}
// End of loadItem method
// Somewhere within the controller:
MyService.loadItem().then(function(item) {
// Perform actions with the item...
alert(item.preprocessed);
}, function(error) {
alert('Error!');
})
Now, the objective is to apply processing to the return value of the promise, while ensuring that rejections are propagated through the promise chain without the need to manually reject the promise at each step.
In the example provided, if the item loads successfully, the 'preprocessed' property is set to true, triggering the appropriate controller handler within the then()
method. However, in case of an error during the loading of the item resulting in a rejection from OtherService.load(), the rejection handler in the controller code does not get executed.
Is there a solution to address this issue? Perhaps there exists a syntax that facilitates the passing of rejections through the chain seamlessly?