JS: Iterator.prototype.take
(new in ECMAScript 2025)
iterator.take(n)-
- Take the first n yield of iterator.
- Return a new Generator.
// define a generator function function* gf() { for (let x of [1, 2, 3, 4, 5]) yield x; } // create a generator. a generator is both iterable and iterator const xgen = gf(); const xresult = xgen.take(3); // verify result console.assert( JSON.stringify(Array.from( xresult, )) === `[1,2,3]`, ); // result is iterable console.assert( Reflect.has(xresult, Symbol.iterator), ); // result is iterator console.assert( Reflect.has(xresult, "next"), );