Sub topic 12
Pitfalls: Single-Use Iterators and Arrays That Are Not Iterators
This chapter assumes you know how to build a pipeline with map, filter, take, and drop, and how to consume it with toArray or spread. It covers two mistakes that catch almost everyone at first. The first is reusing an iterator that has already been consumed. The second is calling helper methods directly on an array.
Iterators are single-use
An iterator is a cursor that only moves forward. Each call to next() moves it along, and it never rewinds. An iterator helper wraps another iterator without copying it, so the whole pipeline shares one cursor.
const evens = [1, 2, 3, 4].values().filter((n) => n % 2 === 0);
console.log([...evens]); // [2, 4]
console.log([...evens]); // []The first spread pulls values until the iterator reports done: true. The second spread starts from that finished state, gets done immediately, and returns an empty array. No error is thrown, which makes the bug easy to miss.
Shared sources advance together
The same problem appears when two pipelines are built on one source iterator. Pulling from either pipeline advances the shared source.
const source = [1, 2, 3, 4].values();
const firstTwo = source.take(2).toArray();
const rest = source.toArray();
console.log(firstTwo); // [1, 2]
console.log(rest); // [3, 4]This output relies on array iterators having no return() method. A generator does have one, so take would close it once its limit was reached, and rest would be []. The chapter on early termination explains this in detail.
The fix: create a fresh iterator each time
If you need to traverse the data more than once, store the iterable (the array, set, or generator function) and build a new pipeline from it whenever you need one. A small factory function is the usual way to do this.
const nums = [1, 2, 3, 4];
const evens = () => nums.values().filter((n) => n % 2 === 0);
console.log([...evens()]); // [2, 4]
console.log([...evens()]); // [2, 4]If you really do need the values twice, call toArray() once and reuse the array.
Arrays are iterable, not iterators
An array is iterable: it has a [Symbol.iterator] method that returns an iterator. The array itself does not inherit from Iterator.prototype, so it has none of the helper methods.
| Expression | What happens |
|---|---|
| `[1, 2, 3].take(2)` | `TypeError`: `take` is not a function |
| `[1, 2, 3].map(f)` | Runs `Array.prototype.map` eagerly and returns a new array |
| `[1, 2, 3].values().map(f)` | Returns a lazy iterator helper |
| `Iterator.from([1, 2, 3]).map(f)` | Returns a lazy iterator helper |
The map row is the subtle one. Arrays already have map and filter, so the code runs without complaint, but it is eager: it builds a full intermediate array at each step. To get lazy behavior, first get an iterator with .values(), .entries(), .keys(), or Iterator.from().
const result = [10, 20, 30, 40]
.values()
.map((x) => x / 10)
.drop(1)
.toArray();
console.log(result); // [2, 3, 4]Exercise
A function receives const it = names.values().map((s) => s.toUpperCase()); and runs console.log(it.toArray().length); console.log(it.toArray().length); with a five-element names array. What does it print, and how would you change the code so both lines print 5?
Show answer
It prints 5 and then 0, because the first toArray exhausts the shared iterator. Fix it by making a factory such as const upper = () => names.values().map((s) => s.toUpperCase()); and calling upper().toArray() each time, or by calling toArray once and reusing the resulting array.
Exercise
Why does [1, 2, 3].filter((n) => n > 1).take(1) throw, while [1, 2, 3].filter((n) => n > 1) alone does not?
Show answer
The receiver is an array, so filter is Array.prototype.filter, which returns another array. Arrays have no take method, so the call throws a TypeError. Insert .values() before filter to use the lazy iterator helpers.