Starters
I'd suggest to look at:
- the ECMAScript Harmony Project's wiki page on Generators,
- the Mozilla Developer Doc on Iterators and Generators,
- the JavaScript Tips page of the jslibs project,
- the Google Traceur Compiler's page on JavaScript Language Features,
- SO questions for "javascript generator yield"SO questions for "javascript generator yield".
A simple example is given in the Mozilla Developer Doc introducing the new features of JavaScript 1.7:
function fib() {
var i = 0, j = 1;
while (true) {
yield i;
var t = i;
i = j;
j += t;
}
}
var g = fib();
for (var i = 0; i < 10; i++) {
console.log(g.next());
}
Others are available in the documents listed above.
Real-World
For real world examples, you can for instance see:
- Google's own Traceur-Compiler's tests for EndYieldGenerator, and SwitchGenerator,
- or even the whole test battery for the
yieldkeyword in Google's traceur-compiler, - or the AcidMonkey tests, for instance this Pi Generator.