Skip to content

Instantly share code, notes, and snippets.

@syg
Created June 19, 2017 20:49
Show Gist options
  • Save syg/da04f29662c6ec0106b2a6f828ccc038 to your computer and use it in GitHub Desktop.
Save syg/da04f29662c6ec0106b2a6f828ccc038 to your computer and use it in GitHub Desktop.
Legacy Generators
=================
Allowed only in versioned JS >= 1.7
The syntax and semantics as currently supported by SpiderMonkey are as follows.
Syntax and static semantics
---------------------------
A plain function that sees `yield` becomes a legacy generator.
It is an early error if a `yield` is seen in the following kinds of function bodies:
- Arrows
- Methods
- Getters
- Setters
- Constructors
- Expression bodies
- It is an early error to use `arguments` in a legacy generator expression.
Runtime semantics
-----------------
4 methods, as below. Does not follow the ES6 iterable protocol. E.g., yielded values are bare and not `{value, done}`.
- `next`
- Like ES6 generator `next`, but throws a `StopIteration` object if generator is already closed. Also throws a `StopIteration` object if generator is already done.
- `send` (alias for `next`)
- `throw`
- Like ES6 generator `throw`
- `close`
- Unlike ES6 generator.close, always returns `undefined`.
Can be used in for-in loops or for-of loops. When used in for-of loops, an automatic shim is applied.
Different closing semantics when used as a legacy iterator in for-in:
- Any exceptions thrown with the legacy generator used as an iterator on the stack closes it.
- Any non-local control flow (break, return) with the legacy generator used as an iterator on the stack closes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment