September 2018
Intermediate to advanced
302 pages
7h 17m
English
Generators are similar to iterators; here, however, you iterate over carefully designed breakpoints within a function. A generator returns an iterator. The returned iterator iterates over the mentioned breakpoints and, each time, returns some value from the function.
To signal that the function is a generator, we use a special * sign, for instance, function* idGenerator(). Please find an example generator function in the following snippet. Generators use the yield keyword to return the current iteration step value. The iterator will resume in the next line if its next() function is invoked, as seen here:
function* numberGenerator(numMax) { for (let i = 0; i < numMax; i += 1) { yield console.log(i); }}const threeNumsI ...
Read now
Unlock full access