strava
strava copied to clipboard
Error handling propagation issue
there is something fishy in the chain saga, service, helper.
-
services are in charge of:
- formatting data to be passed to an helper,
- calling helpers,
- formatting returned data from that helper.
-
helpers are generally based promise,
-
this is exactly the same for services.
the problem is:
- what if an helper reject a promise?
- how is that handled in services?
PS: Be aware of what is the return value of .then(): resolve or failure. Considering the returned value of .then() in both case, we have done something wrong here! :zambia:
As we discussed, something should be done using fp (Monads, ...). see: https://medium.com/javascript-scene/javascript-monads-made-simple-7856be57bfe8 http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
{ const composeM = chainMethod => (...ms) => ( ms.reduce((f, g) => x => g(x)chainMethod) ); const composePromises = composeM('then'); const label = 'API call composition'; // a => Promise(b) const getUserById = id => id === 3 ? Promise.resolve({ name: 'Kurt', role: 'Author' }) : undefined ; // b => Promise(c) const hasPermission = ({ role }) => ( Promise.resolve(role === 'Author') ); // Compose the functions (this works!) const authUser = composePromises(hasPermission, getUserById); authUser(3).then(trace(label)); // true }