Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by wruza on Thursday August 06, 2020 @07:32AM
(#24070025)
It may be the reason, but not the only option.
Error and immediate cb invokation handling omitted for clarity. Similar wrapper or wrapper-generator (uncps(f), unpromise(f)) may be done for other primitives.
[ Parent
| on HN ]
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by cookiengineer on Thursday August 06, 2020 @07:43AM
(#24070072)
> Error and immediate cb invokation handling omitted for clarity.
... but this is exactly the reason why callback hell existed. Error handling down the callback branches were a total mess, especially with networked state transfers.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by wruza on Thursday August 06, 2020 @12:39PM
(#24072630)
Error handling can be easily added to the example wrapper above, completely hiding all the burden from a user of foo(), who then could do their usual try-catch. It's just my phone keyboard that prevents me from implementing it right here.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by gridlockd on Thursday August 06, 2020 @08:45AM
(#24070387)
Sure, it's not the only option, just the better option for Javascript. Your example isn't representative of Javascript.
This is an example for something that might come up in Javascript, using async/await:
const a = await fetchA();
const b = await fetchB(a);
const stuff = await fetchStuff(a, b);
const transformedStuff = await Promise.all(stuff.map(someAsyncTransform));
Error handling is just try/catch.Note that you generally have no control over most of these function being asynchronous. Threads or coroutines do nothing to help you write straightforward code when all your libraries are based on fine-grained callbacks/promises. Writing out that chain of dependencies in CPS would be a nightmare.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by anonymoushn on Thursday August 06, 2020 @02:05PM
(#24073720)
They really do though. In the modal case you would just write the same code as in js but without all the extra keywords. If you need fanout you'd call some special function to do that, like you do now.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by merb on Thursday August 06, 2020 @08:45AM
(#24070386)
well coroutines are a way better option and working way better than promises, unfortunatly promises were introduced since it's easier to convert a callback function to a promise based one