I have a generator that yields 1000 items. I noticed that when I use a for...of
loop to partially consume the generator and then break out of the loop, the iterator seems to close. As a result, my subsequent for...of
loop doesn’t resume where the first one left off.
Here’s the code:
function* test() {
const array = Array.from({ length: 1000 }, (_, index) => index);
for (let item of array) {
yield item;
}
}
const iterator = test();
console.log('Before first loop', iterator);
let j = 0;
for (const i of iterator) {
console.log('A', i);
if (j++ === 3) {
break; // Break after consuming 4 items
}
}
console.log('Before second loop', iterator);
j = 0;
for (const i of iterator) {
console.log('B', i);
if (j++ === 3) {
break;
}
}
and here is the log:
Before first loop test {<suspended>}
A 0
A 1
A 2
A 3
Before second loop test {<closed>}
I expected the second loop to pick up where the first one left off, but it doesn’t. From my understanding, for...of
works with iterators and should resume the iteration unless explicitly reset.
- Why does breaking out of the first
for...of
loop close the iterator? - How exactly
does for...of
interact with the generator’s lifecycle? - Is there a way to prevent this closure without switching to
next()
?
I understand I could manually call next()
on the generator, but I’m specifically asking about how for...of
works in this context.