Open this example in the interactive library
Source code
function* ids() {
for (let id = 1; ; id++) yield id;
}
console.log(ids().take(4).toArray());
Sample output
[ 1, 2, 3, 4 ]

Take a finite prefix from an endless iterator in JavaScript.
Open this example in the interactive library
function* ids() {
for (let id = 1; ; id++) yield id;
}
console.log(ids().take(4).toArray());
[ 1, 2, 3, 4 ]