Sub topic 14
Wrapping Any Iterator with Iterator.from
This chapter assumes you know the iteration protocol, which objects inherit from Iterator.prototype, and how to chain map, filter, take, and drop. It covers what to do when the value you have does not have those helpers.
The Problem: Iterators Without Helpers
Generators and built-in iterators inherit from Iterator.prototype, so they get the helpers automatically. Some iterators don't. Code written before ES2025, iterators from older libraries, and hand-written objects with a next() method all follow the protocol, but their prototype chain never reaches Iterator.prototype.
const iteratorLike = {
i: 0,
next() {
return this.i < 3
? { value: this.i++, done: false }
: { value: undefined, done: true };
},
};
console.log(typeof iteratorLike.map); // undefinedCalling iteratorLike.map(...) throws a TypeError, even though the object works fine as an iterator.
The Fix: Iterator.from
Iterator.from is a static method. It takes an iterable or an iterator-like object and returns an iterator that has all the helper methods.
const iteratorLike = {
i: 0,
next() {
return this.i < 3
? { value: this.i++, done: false }
: { value: undefined, done: true };
},
};
const out = Iterator.from(iteratorLike).map((x) => x + 1).toArray();
console.log(out); // [1, 2, 3]The returned wrapper passes every next() call through to your object, so laziness is kept. The wrapper reads values only when the pipeline pulls them.
How Iterator.from Decides What to Do
Iterator.from follows a short set of rules:
- If the argument has a
[Symbol.iterator]method, it calls that method to get an iterator. - If not, it treats the argument itself as the iterator and uses its
nextmethod. - If the resulting iterator already inherits from
Iterator.prototype, it returns that iterator unchanged. Otherwise it returns a wrapper that does inherit from it.
Because of these rules, you can pass in almost anything that can be iterated:
const arrIter = [1, 2, 3].values();
console.log(Iterator.from(arrIter) === arrIter); // true
console.log(Iterator.from([1, 2, 3]).take(2).toArray()); // [1, 2]
console.log(Iterator.from(new Set(['a', 'b'])).toArray()); // ['a', 'b']
console.log(Iterator.from('hi').toArray()); // ['h', 'i']Strings are the only primitive it accepts. Passing a number, null, or undefined throws a TypeError.
Common Errors
| Mistake | Result | Fix |
|---|---|---|
| Calling `.map` on a legacy iterator | `TypeError: map is not a function` | Wrap it with `Iterator.from` first |
| `Iterator.from(42)` | `TypeError` | Pass an iterable, an iterator, or a string |
| Expecting a fresh copy | The same iterator object comes back | Remember that wrapping does not reset or clone the iterator |
The last row matters because Iterator.from never copies anything. If the original iterator has already been partly consumed, the wrapper starts from the iterator's current position.
Exercise
What does this log, and why? const it = [10, 20, 30].values(); it.next(); console.log(Iterator.from(it).toArray());
Show answer
It logs [20, 30]. An array iterator already inherits from Iterator.prototype, so Iterator.from returns the same object. That object has already given out 10, so toArray collects only the remaining values.
Exercise
Given const legacy = { n: 0, next() { return this.n < 4 ? { value: this.n++, done: false } : { value: undefined, done: true }; } }, write an expression that returns the even values as an array.
Show answer
Iterator.from(legacy).filter((x) => x % 2 === 0).toArray(), which returns [0, 2].