I wonder why this code outputs the same value every time. Seems like nothing's wrong here.
function* infiniteList() {
let count = 0
while (true) yield count++
}
for (var x = 0; x < 100; x++) console.log(infiniteList().next().value)
I wonder why this code outputs the same value every time. Seems like nothing's wrong here.
function* infiniteList() {
let count = 0
while (true) yield count++
}
for (var x = 0; x < 100; x++) console.log(infiniteList().next().value)
You are re-creating your iterator in every loop iteration.
You need to create it once and then reuse it to get the next element:
function* infiniteList() {
let count = 0
while (true) yield count++
}
let mylist = infiniteList();
for (let x = 0; x < 100; x++) console.log(mylist.next().value)