In the previous post, we’ve covered what iterators are, and how JS defines a protocol for them. We’ve seen that they provide a one-shot traversal over a collection. But they require a lot of boilerplate to make them work.
This post introduces the iterable protocol, which is a standardized way to make iterators.
Just like with iterators, making an object to conform to the iterable protocol is just a matter of adding a function. Its name is
Symbol.iterator, and it needs to be zero-arity and has to return an iterator. Think of it as a multiple-use factory to make one-shot iterators.
To wrap the iterator from the previous post into an iterable (Try it):
Using this iterable, the 1, 2, and 3 elements can be iterated over and over again:
Think of iterables as collections that can be iterated over, multiple times.
There are language constructs that make this iteration a lot friendlier. The two example we’ll look into are the
for..of loop and array destructuring.
There are also some functions that accept iterables, for example, the
While not all third-party libraries that provide collections embrace the iterable protocol, some do. For example, ImmutableJS collections are all iterables, making them compatible with the
Also, you can use any iterable to construct Immutable Lists:
In effect, wherever you expect an iterable, you can use Arrays, custom-built iterables, Immutable collections, and any other compatible third-party data types.
Iterables are what make iterators actually useful. They abstract away the essence of collections, that is the ability to produce elements. If you need just that, different implementations become interchangeable on the language level.
On the downside, hand-writing iterables is still tedious.
In the next post, you’ll learn the basics of generators, which are a special kind of functions that make this syntax a lot more developer-friendly.