5 ms·
I tried to implement async/await in javascript staying closer to CPS form without the extra caching layers and state machines (without the promises) that normal
by BenoitEssiambre 6y ago
I tried to implement async/await in javascript staying closer to CPS form without the extra caching layers and state machines (without the promises) that normally wrap every async calls.
https://www.npmjs.com/package/casync https://www.npmjs.com/package/casync
I use it all the time. Except for the lack of language level tooling (that you get with promise wrapped async/await), it works quite well and it's faster than native async/await. I wish something like it would be integrated in the language.
- dandanua 6y agoVery cool! I was always wondering why a single-threaded language doesn't transform async/await into a "callback heaven" under the hood. It should be much easier than what Rust did to garbage collection.
- Twisol 6y agoExceptions kind of muck with things, since you want to be able to establish handlers further up the stack. Compiling async/await to callbacks means also compiling exceptions down to a different mechanism, which makes it difficult to get a stack trace and so on.
- BenoitEssiambre 6y agoNote that casync gives you somewhat ok stack traces (with language level support it could be better) for example: const {casync} = require('./casync.js'); let inner=casync(function*(next){ throw new Error("test err"); }); let outer=casync(function*(next){ yield inner(next); }); outer((err)=>{throw err;}); Gives you Error: test err at /home/benoit/Documents/casync/errorTest.js:4:8 at Generator.next (<anonymous>) at doNext (/home/benoit/Documents/casync/casync.js:26:12) at next (/home/benoit/Documents/casync/casync.js:44:5) at /home/benoit/Documents/casync/casync.js:49:3 at /home/benoit/Documents/casync/errorTest.js:8:8 at Generator.next (<anonymous>) at doNext (/home/benoit/Documents/casync/casync.js:26:12) at next (/home/benoit/Documents/casync/casync.js:44:5) at /home/benoit/Documents/casync/casync.js:49:3 If you skip the lines inside casync which are in most case irrelevant you get: Error: test err at /home/benoit/Documents/casync/errorTest.js:4:8 at /home/benoit/Documents/casync/errorTest.js:8:8