This library implements delimited control operators as a very thin (50 LOC) layer over plain ES6 generators.
It doesn’t add any expressive power over plain generators, but I think it offers a more intuitive API. Specifically, tasks like waiting for a promise to settle, spawning a new independent generator, or implementing a blocking event loop become straightforward.
With a small constant overhead (having to use function*
, using
yield*
for every blocking call, and using next()
to kickstart
generators from an ordinary, non-generator function), one can write
asynchronous JS programs in a quasi-direct style.
Check out the introductory blog post.
A single generator that runs in an infinite loop. Each time through the loop it prints something and then sleeps. Demonstrates the core API.
Demonstrates spawning multiple generators with their own independent loops.
Demonstrates a blocking main event loop, and multiple independent generators.
Suspends the currently running generator, and calls the suspension handler in the place where the generator was originally run. The suspension handler receives the suspended generator as its single argument.
In terms of delimited control, this library implements single-prompt continuations that can only be resumed once (this is a fundamental limitation of generators, but personally I haven’t needed multiply-resumable continuations yet).
Multiple prompts could be added quite easily I think, but I haven’t really needed those either, yet.
The API functions run
, suspend
, and resume
roughly correspond to
push_prompt
, take_subcont
, and push_delim_subcont
, respectively,
from Oleg’s
caml-shift
library.