yield ;
The yield statement turns a function into a generator which generates a list of values. Each yield generates a single value.
When the generator is called, it returns an iterator, which is an object with the method next(). On the first call, it executes the function until the first yield, and returns the yielded value. On the next call to next(), execution of the function continues immediately after the yield. The state of local variables are preserved between calls to next().
Example:
function g() { yield 10; yield 20; yield 30; } var i = g(); i.next(); --> returns 10 i.next(); --> returns 20 i.next(); --> returns 30 i.next(); --> throws the StopIteration exception
Iterators can be used like arrays in for each loops. Example:
function g() { yield 10; yield 20; yield 30; } for each (x in g()) console.log(x); // logs 10, 20, 30