Synchronous iteration is a protocol (interfaces plus rules for using them) that connects two groups of entities in JavaScript:
Data sources: On one hand, data comes in all shapes and sizes. In JavaScript’s standard library, you have the linear data structure Array, the ordered collection Set (elements are ordered by time of addition), the ordered dictionary Map (entries are ordered by time of addition), and more. In libraries, you may find tree-shaped data structures and more.
Data consumers: On the other hand, you have a whole class of constructs and algorithms that only need to access their input sequentially: one value at a time, until all values were visited. Examples include the for-of
loop and spreading into function calls (via ...
).
The iteration protocol connects these two groups via the interface Iterable
: data sources deliver their contents sequentially “through it”; data consumers get their input via it.
for-of
loop use the interface Iterable
. Data sources such as Arrays
implement that interface.The diagram in fig. 18 illustrates how iteration works: data consumers use the interface Iterable
; data sources implement it.
The JavaScript way of implementing interfaces
In JavaScript, an object implements an interface if it has all the methods that it describes. The interfaces mentioned in this chapter only exist in the ECMAScript specification.
Both sources and consumers of data profit from this arrangement:
If you develop a new data structure, you only need to implement Iterable
and a raft of tools can immediately be applied to it.
If you write code that uses iteration, it automatically works with many sources of data.
Two roles (described by interfaces) form the core of iteration (fig. 19):
Iterable
and Iterator
. The former has a method that returns the latter.These are type definitions (in TypeScript’s notation) for the interfaces of the iteration protocol:
interface Iterable<T> {
[Symbol.iterator]() : Iterator<T>;
}
interface Iterator<T> {
next() : IteratorResult<T>;
}
interface IteratorResult<T> {
value: T;
done: boolean;
}
The interfaces are used as follows:
Iterable
for an iterator via the method whose key is Symbol.iterator
.Iterator
returns the iterated values via its method .next()
..value
is the iterated value..done
indicates if the end of the iteration has been reached, yet. It is true
after the last iterated value and false
beforehand.This is an example of using the iteration protocol:
const iterable = ['a', 'b'];
// The iterable is a factory for iterators:
const iterator = iterable[Symbol.iterator]();
// Call .next() until .done is true:
assert.deepEqual(
iterator.next(), { value: 'a', done: false });
assert.deepEqual(
iterator.next(), { value: 'b', done: false });
assert.deepEqual(
iterator.next(), { value: undefined, done: true });
while
The following code demonstrates how to use a while
loop to iterate over an iterable:
function logAll(iterable) {
const iterator = iterable[Symbol.iterator]();
while (true) {
const {value, done} = iterator.next();
if (done) break;
console.log(value);
}
}
logAll(['a', 'b']);
// Output:
// 'a'
// 'b'
Exercise: Using sync iteration manually
exercises/sync-iteration-use/sync_iteration_manually_exrc.mjs
We have seen how to use the iteration protocol manually and it is relatively cumbersome. But the protocol is not meant to be used directly – it is meant to be used via higher-level language constructs built on top of it. This section shows what that looks like.
JavaScript’s Arrays are iterable. That enables us to use the for-of
loop:
const myArray = ['a', 'b', 'c'];
for (const x of myArray) {
console.log(x);
}
// Output:
// 'a'
// 'b'
// 'c'
Destructuring via Array patterns (explained later) also uses iteration, under the hood:
JavaScript’s Set data structure is iterable. That means, for-of
works:
const mySet = new Set().add('a').add('b').add('c');
for (const x of mySet) {
console.log(x);
}
// Output:
// 'a'
// 'b'
// 'c'
As does Array-destructuring:
The following built-in data sources are iterable:
To iterate over the properties of objects, you need helpers such as Object.keys()
and Object.entries()
. That is necessary, because properties exist at a different level that is independent of the level of data structures.
The following constructs are based on iteration:
Destructuring via an Array pattern:
The for-of
loop:
Array.from()
:
Spreading (via ...
) into function calls and Array literals:
new Map()
and new Set()
:
Promise.all()
and Promise.race()
:
yield*
:
Quiz
See quiz app.