|
|
|
|
|
by forgotpwtomain
3667 days ago
|
|
> If you're not handling the error in the first promise, then throw it again. You are handling the error (e.g. providing the user with a flash message telling them their was an error) - but if you don't re-throw it you will end up in the 'success' callback-chain - without a result obviously. Therefore whether you re-throw the error or squash it depends on whether another 'reject' function will be there to squash it further down the chain etc. |
|
I can think of a couple cases where catching an error down low with the purpose of squashing it makes sense. One would be for a service that you know may not have data and you don't care if it doesn't. For instance, trying to get geolocation data for a user in order to improve their experience but if it fails you don't need to show an error:
return geolocationService.fetch().catch((err) { /* log error /; return {}; / empty result/ });
Another case is when you might have multiple ways of handling the request where one is prioritized over the other. In this case, you can catch errors from the first attempt and try a second method. If they both error out, then the result promise will be an error as well.
return primary.fetch().catch((err) => { return secondary.fetch(); }).then((data) => { / manipulate data */});