Loading...
function* ids() {
let id = 1;
while (true) yield id++;
}
const first = [...ids()].slice(0, 3);function* ids() {
let id = 1;
while (true) yield id++;
}
const first = ids().take(3).toArray(); // [1, 2, 3]Spreading an infinite generator never ends; take() stops after three values, so the array can be built.
toArray() on an infinite generator before take(), which hangs the page.take() before toArray() or a loop.take() throws a RangeError.take() close the source iterator?Yes. Once the limit is reached, take() calls return() on the source, so a generator's finally block runs.
take()?take() yields the values that exist and finishes, without an error.