This is a JS-port of 'coro' library from "Revisiting Coroutines" (Ana Lúcia de Moura and Roberto Ierusalimschy, 2004)
Simple example for Producer/Consumer
const coro = require("symcoro");
const produce = coro.create(function* produce(n) {
yield* coro.transfer(consume, n * 3);
});
const consume = coro.create(function* consume(v) {
yield* coro.transfer(coro.main, v * 2)
});
coro.run(function* () {
console.log(yield* coro.transfer(p, 7)); // 42
});
Notice that main
coroutine transfers control to the produce
coroutine but gains it back from consume
coroutine.
coro.create(f, name?)
creates a coroutine with a given generator function.
f
: A generator functionname
: Optional. Useful for debug output- Returns a coroutine
coro.transfer(co, val)
transfers control to another coroutine. The coroutine will be
resumed with the passed val
. The current coroutine will be suspended.
co
: A coroutine which will be resumedval
: Optional- Returns when this coroutine is resumed
coro.run(f, callback)
runs a generator function as a main
coroutine. When f
returns a value, callback
will be called with it.
f
: A generator functioncallback
: Optional
There are some coroutines you can pass control back and forth.
coro.main
: You can transfer control to the main coroutine which is a generator function passed tocoro.run(f)
.coro.current
: You can pass the current coroutine as an argument when transfering control to another coroutine.coro.parent
: This coroutine refers the coroutine which created the current coroutine.