JavaScript · Middle · Question 27 of 75
What does a generator function do?
function* ids() {
let id = 1;
while (true) yield id++;
}
const gen = ids();
gen.next().value; // 1- ✓Correct answer: Returns an iterator and runs in steps, pausing at
yield - 2Runs asynchronously in a separate thread and returns a promise
- 3Generates new function code while the program is running
Explanation
Calling a generator doesn't run its body; it returns an object with a next() method. Each next() continues until the next yield. Generators are handy for lazy and infinite sequences and for implementing iterators.