Re:What does `return function *(){…}` mean in JavaScript?
by ynniv on Thursday November 07, 2013 @03:31PM
(#6692183)
ES6 generators are (shallow) coroutines
This is the first I've heard ES6 generators being coroutines. Is this documented somewhere other than the PEP?
Re:What does `return function *(){…}` mean in JavaScript?
by masklinn on Thursday November 07, 2013 @05:37PM
(#6692948)
I'm not sure what you're looking for exactly. The generators proposal defines the `send` method[0] (as well as `throw`) and its semantics. They're not "full" coroutines as they can't yield to an arbitrary coroutine (unless a trampoline is used), only to their parent/caller.
[0] http://wiki.ecmascript.org/doku.php?id=harmony:generators#me...
And yet it's perfectly valid. ES6 generators are (shallow) coroutines (or "extended generators" in Python-land[0]). From the outside, they provide a `send()` method as the converse to `next()`, and that method is used to inject a value back in the generator, via `yield` as an expression. And yes you can use the same yield to send a value out and retrieve one in the same "yield step".
That is how e.g. taskjs[1] works:
the first line sends a promise to the scheduler and suspends the generator, the scheduler then resumes the generator with send() to inject the resolution of the promise back in.> The meaning of a symbol can often be inferred from its context - it doesn't need to be a reserved word.
Not without function * . the purpose of function * is precisely to provide the context for `yield` being not-a-name.
> Yes, it would not be allowable to have an open paren after yield.
You do realize how utterly insane that reads right? You propose crippling `yield` and making it significantly less convenient and sensible to get rid function * (and the colon does not work, it conflicts with labels)
[0] http://www.python.org/dev/peps/pep-0342/
[1] http://taskjs.org/