http://axisofeval.blogspot.com/2024/05/delimited-generators.html skip to main | skip to sidebar The Axis of Eval Manuel Simoni's blog about programming (languages). Thursday, May 2, 2024 Delimited Generators - A more natural API for JS generators I have been studying ways to work around the [S:horrors:S] issues of JavaScript's async APIs for years. I have even built a series of increasingly elaborate continuation-based Lisp interpreters (here's the latest one, it's quite good, if I may say so). But recently I finally came to the point where I understood JS generators well enough to realize that generators already solve the problem! With a small constant syntactic overhead (having to use function* to define generators, having to use yield* to call them, and using next() to call them from non-generator functions), one can program asynchronous code in a quasi-direct style. But the plain generator interface is rather low-level, and not very intuitive to use. So I built delimgen, a thin layer on top of plain generators, that mimics delimited control. Delimited control is initially hard to understand, but once you grok it it's a very natural approach (previous post). Here's how a simple delimited generator looks like. See it in action here. You can easily spawn multiple independent generators. See it in action here. You can also do blocking event loops easily. See it in action here. I'm not claiming any novelty here. You also cannot do anything with this library that you couldn't do with plain generators, but for me, seeing that you can write quasi-blocking code in JS with some small overhead was a real eye opener. Update: Thanks to mmastrac on HN for pointing out that this leaks stack - back to the drawing board! (also: Lobsters, with an alternative formulation using async/await by easrng) Posted by Manuel Simoni at 11:23 # No comments: Post a Comment Older Post Home Subscribe to: Post Comments (Atom) Elsewhere @msimoni github.com/manuel Archives * V 2024 (2) + V May (1) o Delimited Generators - A more natural API for JS g... + > January (1) * > 2016 (2) + > July (1) + > May (1) * > 2015 (4) + > August (1) + > July (1) + > June (2) * > 2014 (2) + > June (2) * > 2013 (8) + > June (1) + > May (4) + > April (1) + > February (1) + > January (1) * > 2012 (85) + > November (11) + > September (5) + > August (8) + > July (23) + > June (2) + > May (4) + > April (9) + > March (13) + > February (2) + > January (8) * > 2011 (94) + > December (5) + > November (5) + > October (5) + > September (7) + > August (13) + > July (9) + > June (7) + > May (9) + > April (9) + > March (9) + > February (6) + > January (10) * > 2010 (102) + > December (9) + > November (8) + > October (5) + > September (13) + > August (7) + > July (16) + > June (22) + > May (14) + > April (8) Linkroll * Alarming Development * Alex Payne -- Online Writing * Arcane Sentiment * Awelon Blue * blog.might.net * Dependent Types Reddit * Dynamic Aspects * eighty-twenty * Existential Type * Hack the Planet * HXA7241 * Kazimir Majorinc's Lisp Notes * Lambda the Ultimate * Linux Weekly News * Loper OS * Luke Gorrie's weblog * Making it stick. * One Mistake After Another * Paul Graham * Paul Snively's Blog * Philosophy Made Manifest * PiLuD: Programming Language Design * Proggit * Real World Technologies Forums * Send More Paramedics * State of Code * Stevey's Blog Rants * Structural insight * Subvert the dominant paradigm * Taylor Campbell's blag * The BileBlog * The ByteBaker * Trivium * Ward's wiki * wingolog