Generators are functions that can generate multiple values on a per-request basis and can suspend their execution between requests.
We can run some code and return a value and go right back into the function at the same place you left it.
function* cars() {
yield 'Focus';
yield 'Fusion';
yield 'F150';
yield 'F250';
yield 'EXPLORER';
}
const gen = cars();
gen.next(); //=> { "value": "Focus", "done":false }
gen.next(); //=> { "value": "Fusion", "done":false }
gen.next(); //=> { "value": "F150", "done":false }
gen.next(); //=> { "value": "F250", "done":false }
gen.next(); //=> { "value": "EXPLORER", "done":false }
gen.next(); //=> { "done":true }
Works perfectly with for of loops :)
You can use it to do async functions...
for (let car of cars()) {
console.log(car);
}
Please, look at the delegation.js file.
Please, look at the symbol.js file.
node index.js
and
node cars.js
and
node delegation.js
and
node symbol.js