2 ms·
It may be the reason, but not the only option. function cpsfoo(cb) {...} function foo() { var thread = this_thread() cpsfoo(x => thread.resume(x))
by wruza 6y ago
It may be the reason, but not the only option.
function cpsfoo(cb) {...}
function foo() {
var thread = this_thread()
cpsfoo(x => thread.resume(x))
return thread.yield()
}
Error and immediate cb invokation handling omitted for clarity. Similar wrapper or wrapper-generator (uncps(f), unpromise(f)) may be done for other primitives.
- merb 6y agowell 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
- gridlockd 6y agoSure, 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.
- anonymoushn 6y agoThey 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.
- cookiengineer 6y ago> 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.
- wruza 6y agoError 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.