JavaScript Generators
Generator functions , allow you to define a function
whose execution is not continuous. Generator functions
are written using the function*
syntax.
Generator function do not initially execute their code.
Instead they return a special type of iterator called
a Generator. When a value is consumed by calling the
generators next
method, then function executes until
it encounters yield
keyword.
function* fooGenerator() {
yield 1;
yield 2;
yield 3;
}
for (const val of fooGenerator()) {
console.log(val); // 1 2 3
}
References:
Categories :
JavaScript
ES6