xxxxxxxxxx
function* gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen(); // "Generator { }"
xxxxxxxxxx
function* myGenerator(start = 0, stop = 100, step = 5) {
for (let i = start; i <= stop; i += step) {
yield i;
}
}
for (let v of myGenerator()) {
console.log(v);
}
xxxxxxxxxx
// define a generator function
function* generator_function() {
..
}
// creating a generator
const generator_obj = generator_function();
generators javascript
xxxxxxxxxx
function* g(){ //or function *g(){}
console.log("First");
yield 1;
console.log("second");
yield 2;
console.log("third");
}
let generator=g();
generator.next(); // Output First
generator.next(); // Output second
Generators compute their yield ed values on demand, which allows them to efficiently represent sequences that are expensive to compute