liveBook · Manning

Iterable

As explained in The Joy of JavaScript

An iterable is an object or value that can be consumed by an iteration protocol to produce a sequence of elements.

An iterable is an object whose elements can be enumerated through JavaScript’s iteration protocol. It defines Symbol.iterator, which supplies an iterator controlling how values reach the caller. The iterator’s next method returns { value, done }, allowing consumers such as for...of to advance until iteration finishes. section 9.1 section 9.6

class Block {

 index = 0;
 constructor(index, previousHash, data = [], difficulty = 0) {
   this.index = index;
   this.previousHash = previousHash;
   this.data = data;
   this.nonce = 0;
   this.difficulty = difficulty;
   this.timestamp = Date.now();
   this.hash = this.calculateHash();
 }

 //...

 [Symbol.iterator]() {
   return this.data[Symbol.iterator]();
 }
}

A custom Block becomes iterable by delegating Symbol.iterator to its internal data array, so callers can consume its transactions directly. section 9.1.3

Iterable and iterator describe related but distinct roles. An iterator provides next; an iterable provides Symbol.iterator and returns an iterator. A random-number iterator therefore becomes usable with for...of only after it returns itself from Symbol.iterator. The same protocol powers the spread operator and supports custom collections such as trees, graphs, dictionaries, and directory traversals. section 9.1.3

Domain objects and generators

Making Blockchain, Block, and Transaction objects iterable enables generic validation with for...of and permits early exit when the first failure appears. Generator functions extend the pattern by producing iterable sequences lazily. A Blockchain generator can yield newly configured blocks indefinitely, while the caller limits consumption with break. Iterables also integrate with destructuring: a Validation object can yield its failure and success branches for concise state extraction. section 9.1.3 section 9.2.2 section 9.2

This protocol separates collection design from consumption. Consumers can use the same for...of, spread, or destructuring syntax regardless of the data type. JavaScript also supports asynchronous counterparts: async iterators return promises from next, and async generators are consumed with for await...of. section 9.1.3 section 9.6

How The Joy of JavaScript covers it

How deep it goes
Explains Symbol.iterator, next, value, done, generators, async iteration, and early termination, but defers complete protocol details.
Examples
Uses blockchain models, transaction blocks, random-number iterators, strings, generated blocks, and Validation branches.
What you can do after
Build it from scratch by implementing Symbol.iterator or a generator, then consume custom sequences with for...of, spread, or destructuring.
How it connects
Connects iterables to iterators, generators, streams, the spread operator, destructuring assignment, and asynchronous iteration.

Also explained in

Compare how 7 books explain iterable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.