Tame.JS: Flow-control by the makers of OkCupid.com
The problem this solves is a serious one, in my experience, even though I find their choice of syntax rather curious. Given that JavaScript 1.7 introduces the <i>yield</i> keyword, it would make sense to add support for that to V8 and implement the infrastructure for concurrent asynchronous I/O around that as a library. The concurrency aspect is, after all, orthogonal to the blocking vs. callback situation, and can easily be done even when using callbacks, with a single callback function called upon completion of all concurrent I/O. I believe the Dojo framework provides such a utility, and I wrote my own simplistic stand-alone mini-library for exactly this a while back. [0]<p>I've run into the problem of endless chained callbacks in C, where it's much worse due to the lack of nested functions, let alone closures or garbage collection.[1] I ended up using the switch block "coroutine" hack [2] for the worst cases, along with dynamically allocated "context" structs to hold "local" variables. A proper macro system would have helped transform blocking code into CPS form. I tried to integrate a SC [3] pass into our build, which could have done it, but ran into all sorts of practical/yak shaving problems, so I ended up with the C preprocessor macro/switch solution for now. In user space, explicit stack-switching with something like <i>swapcontext()</i> is probably preferable, if you can get away with it, but in the kernel this is rather problematic.<p>[0] <a href="https://github.com/pmj/MultiAsync-js" rel="nofollow">https://github.com/pmj/MultiAsync-js</a><p>The reason I wrote my own was because I originally needed it in Rhino, the JVM-based JS implementation, and I couldn't find anything similar that worked there.<p>[1] Yes, there are garbage collectors that work with C, but to my knowledge, none of them can be used in kernel modules. In any case, the other 2 issues are worse and aren't solveable within the language via libraries.<p>[2] <a href="http://www.linuxhowtos.org/C_C++/coroutines.htm" rel="nofollow">http://www.linuxhowtos.org/C_C++/coroutines.htm</a><p>[3] <a href="http://super.para.media.kyoto-u.ac.jp/~tasuku/sc/index-e.html" rel="nofollow">http://super.para.media.kyoto-u.ac.jp/~tasuku/sc/index-e.htm...</a>