Ask HN: JS's for-of loop conceptually flawed, or is it just me?
When working with generators, two identical for-of loops don't show the same behaviour.
Specifically, the first one will behave as expected, whereas the second one will terminate immediately.
What is this?
I know that the iterator `g` will signal the value `done` after the first loop, but this behaviour was unexpected and really counterintuitive to me
Note: contrary to this example, for-of loops of arrays will work as I expected. It just doesn't make sense
Code example:
> function* gen() { yield* [1,2,3] }
undefined
> let g = gen()
undefined
> for (let el of g) console.log(el)
1
2
3
undefined
> for (let el of g) console.log(el)
undefined
edit: layout
9 comments
[ 5.3 ms ] story [ 36.5 ms ] threadOr make a second instance of the generator.
The second option could be a solution, but the specific case was reusing a variable `const nums = iu.iterate(x => x+1, 1)`, and quite extensively. (It's just a helper function to make a list of all natural numbers)
It would be so much nicer if I could just reuse this variable throughout my code
Otherwise there are constructions that provide a virtual copy of an iterator and cache the values they still need for the second loop.
Or just don't use an iterator if it doesn't fit the model you need, or turn it into an array before.
(Part of) the idea behind generators is that you don't store all results, because they could be a long or possibly even infinite sequence. E.g. in Python the basic iterator is range(X), which returns the numbers from 0 to X-1. If X=100000, it is a bad idea to generate a giant list of all those numbers, just because you want to know how often you ran through a loop.
edit: I just thought, I could make a function, bind that to a variable and regen the generator again and again