External: Generator

Generator

Generator functions.

They are actually a simple function which returns an iterator and can be implemented in duck typing.

Source:
See:

Examples

function-star notation

function* generator() {
 yield 1;
 yield 2;
 yield 3;
}

Duck typing

function generator() {
 var i = 0;
 var content = [1, 2, 3];
 return {
   next : function () {
     var res;
     if (i < content.length) {
       res = { done : false, value : content[i] };
     } else {
       res = { done : true };
     }
     i++;
     return res;
   }
 };
}