JavaScript and TypeScript course ยท Module 4: Asynchronous JavaScript
Iterators and Generators
In this lesson6
A dinosaur genome has billions of base pairs. If the Jurassic Park lab loaded the whole sequence at once, the computers would crash before anyone read the first gene. So the scientists analyze DNA fragment by fragment, on demand. Iterators and generators in JavaScript work the same way: they let you process data element by element, lazily and economically. "It is the lab's assembly line," says Dr. Rex. "You get the next sample exactly when you need it."
The Iterable Protocol
Any object that has a [Symbol.iterator]() method is iterable and can go into a for...of loop. Arrays and strings have it from birth:
// Arrays, strings, Map, Set - are iterable by nature
for (const char of 'ATCG') {
console.log(char); // A, T, C, G
}
for (const item of [1, 2, 3]) {
console.log(item); // 1, 2, 3
}Under the hood for...of calls [Symbol.iterator]() and gets an iterator, an object with a next() method. You can call it by hand:
const it = ['T-Rex', 'Raptor'][Symbol.iterator]();
console.log(it.next()); // { value: 'T-Rex', done: false }
console.log(it.next()); // { value: 'Raptor', done: false }
console.log(it.next()); // { value: undefined, done: true }Every call returns an object with the fields value and done. The loop ends when done is true, and the value of that last result is skipped.
Creating a Custom Iterator
A plain object is not iterable. To walk around a paddock with a loop, we add a [Symbol.iterator]() method that returns an object with next():
const dinoEnclosure = {
dinosaurs: ['Rex', 'Blue', 'Delta', 'Echo'],
[Symbol.iterator]() {
let index = 0;
const dinos = this.dinosaurs;
return {
next() {
if (index < dinos.length) {
return { value: dinos[index++], done: false };
}
return { value: undefined, done: true };
}
};
}
};
// Now we can use for...of!
for (const dino of dinoEnclosure) {
console.log('Dinosaur:', dino);
}
// And the spread operator
const allDinos = [...dinoEnclosure];
console.log(allDinos); // ['Rex', 'Blue', 'Delta', 'Echo']The index counter lives in a closure, so every new loop starts from zero. Spread works right away, because it uses the same protocol as for...of.
for...of vs for...in
These two loops have similar names but completely different jobs. Compare them on an object and on an array:
const park = { name: 'Jurassic', size: 'large', open: false };
const species = ['T-Rex', 'Velociraptor', 'Triceratops'];
// for...in - iterates over KEYS (of objects and arrays)
for (const key in park) {
console.log(key); // 'name', 'size', 'open'
}
for (const index in species) {
console.log(index); // '0', '1', '2' (strings!)
}
// for...of - iterates over VALUES (of iterables)
for (const animal of species) {
console.log(animal); // 'T-Rex', 'Velociraptor', 'Triceratops'
}
// for...of does NOT work on plain objects!
// for (const val of park) {} // TypeError!
// But it works on Object.entries():
for (const [key, val] of Object.entries(park)) {
console.log(key, val);
}for...in returns keys as strings and also visits properties inherited from the prototype. My advice: walk arrays with for...of, and objects with for...of over Object.entries().
Generators (function*)
Writing next() by hand is tedious. A generator does the bookkeeping for you: it is a function marked with an asterisk, function*, that can pause at yield and resume later:
function* dinoGenerator() {
console.log('Generating first dinosaur...');
yield 'T-Rex';
console.log('Generating second...');
yield 'Velociraptor';
console.log('Generating third...');
yield 'Triceratops';
console.log('Generation complete!');
}
const gen = dinoGenerator();
console.log(gen.next()); // { value: 'T-Rex', done: false }
console.log(gen.next()); // { value: 'Velociraptor', done: false }
console.log(gen.next()); // { value: 'Triceratops', done: false }
console.log(gen.next()); // { value: undefined, done: true }Calling dinoGenerator() prints nothing; it only returns a generator object. The code starts at the first next() and runs to the nearest yield, so each message appears right before its result. A generator can also finish with return:
function* dinos() {
yield 'T-Rex';
yield 'Raptor';
return 'done';
}
const herd = dinos();
console.log(herd.next()); // { value: 'T-Rex', done: false }
console.log(herd.next()); // { value: 'Raptor', done: false }
console.log(herd.next()); // { value: 'done', done: true }
console.log([...dinos()]); // ['T-Rex', 'Raptor'] - without 'done'The value from return arrives together with done: true, which is why for...of and spread skip it.
Infinite Generator
A generator may never finish, because it computes the next value only on request:
function* idGenerator(prefix) {
let id = 1;
while (true) {
yield prefix + '-' + String(id).padStart(3, '0');
id++;
}
}
const dinoIds = idGenerator('DINO');
console.log(dinoIds.next().value); // 'DINO-001'
console.log(dinoIds.next().value); // 'DINO-002'
console.log(dinoIds.next().value); // 'DINO-003'
// Never ends - generates on demand!The while (true) loop does not freeze the program, because execution stops at every yield. Never unpack such a generator with spread, though: [...dinoIds] would try to collect infinitely many values.
yield* - Delegation to Another Generator
yield* passes along, one by one, every value of another generator or iterable, so small generators can be combined into larger ones:
function* carnivores() {
yield 'T-Rex';
yield 'Velociraptor';
}
function* herbivores() {
yield 'Triceratops';
yield 'Brachiosaurus';
}
function* allDinosaurs() {
yield* carnivores();
yield* herbivores();
}
for (const dino of allDinosaurs()) {
console.log(dino);
}
// T-Rex, Velociraptor, Triceratops, BrachiosaurusSince yield* also works on arrays, you can shorten the paddock's custom iterator to one line: *[Symbol.iterator]() { yield* this.dinosaurs; }. That is how I recommend writing it.
Async Generators
Data from a server arrives in pages, and each page needs await. An async generator combines async with function*, and its results are received by a for await...of loop:
async function* fetchDinoPages(apiUrl) {
let page = 1;
let hasMore = true;
while (hasMore) {
const response = await fetch(apiUrl + '?page=' + page);
const data = await response.json();
yield data.results;
hasMore = data.hasNextPage;
page++;
}
}
// Usage with for await...of
async function loadAllDinos() {
for await (const batch of fetchDinoPages('/api/dinosaurs')) {
console.log('Loaded batch:', batch.length, 'dinosaurs');
}
}The next page loads only when the loop asks for the next batch. If nobody processes the results, no extra request goes out to the server.
Practical Applications
The first classic is lazy evaluation: the Fibonacci sequence is infinite, and we take only as much of it as we need:
// 1. Lazy evaluation - process on demand
function* fibonacci() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
// Take only the first 10
const fib = fibonacci();
const first10 = [];
for (let i = 0; i < 10; i++) {
first10.push(fib.next().value);
}
console.log(first10); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]The line [a, b] = [b, a + b] is the destructuring swap you know from module 2. The second classic is pagination, serving data in portions:
// 2. Pagination - load pages on demand
function* paginate(items, pageSize) {
for (let i = 0; i < items.length; i += pageSize) {
yield items.slice(i, i + pageSize);
}
}
const allSpecies = ['A','B','C','D','E','F','G','H'];
const pages = paginate(allSpecies, 3);
console.log(pages.next().value); // ['A', 'B', 'C']
console.log(pages.next().value); // ['D', 'E', 'F']
console.log(pages.next().value); // ['G', 'H']The last page has only two elements, because slice stops at the end of the array. The same pattern, a loop with a step and yield, is all you need to write your own range(start, end, step).
Practice
The sandbox contains a custom paddock iterator and generators to practice with. Call next() by hand and predict when done: true will appear.
Code for this lesson: index.js
// === ITERATORS AND GENERATORS ===
// 1. Custom iterator
const enclosure = {
dinosaurs: ['Rex', 'Blue', 'Delta', 'Echo'],
[Symbol.iterator]() {
let i = 0;
const dinos = this.dinosaurs;
return {
next() {
return i < dinos.length
? { value: dinos[i++], done: false }
: { done: true };
}
};
}
};
console.log('=== Iterator ===');
for (const dino of enclosure) {
console.log('Dino:', dino);
}
console.log('Spread:', [...enclosure]);
// 2. for...of vs for...in
console.log('\n=== for...of vs for...in ===');
const species = ['T-Rex', 'Raptor', 'Triceratops'];
console.log('for...in (keys):');
for (const i in species) console.log(' ', i, typeof i);
console.log('for...of (values):');
for (const s of species) console.log(' ', s);
// 3. Generator
function* dinoGen() {
yield 'T-Rex';
yield 'Velociraptor';
yield 'Triceratops';
}
console.log('\n=== Generator ===');
const gen = dinoGen();
console.log(gen.next()); // { value: 'T-Rex', done: false }
console.log(gen.next()); // { value: 'Velociraptor', done: false }
console.log(gen.next()); // { value: 'Triceratops', done: false }
console.log(gen.next()); // { done: true }
// 4. Infinite generator
function* idGen(prefix) {
let id = 1;
while (true) {
yield prefix + '-' + String(id++).padStart(3, '0');
}
}
console.log('\n=== ID Generator ===');
const ids = idGen('DINO');
console.log(ids.next().value); // DINO-001
console.log(ids.next().value); // DINO-002
console.log(ids.next().value); // DINO-003
// 5. yield* - delegation
function* carnivores() { yield 'T-Rex'; yield 'Raptor'; }
function* herbivores() { yield 'Triceratops'; yield 'Brachio'; }
function* allDinos() { yield* carnivores(); yield* herbivores(); }
console.log('\n=== yield* ===');
console.log([...allDinos()]);
// 6. Pagination with a generator
function* paginate(items, size) {
for (let i = 0; i < items.length; i += size) {
yield items.slice(i, i + size);
}
}
console.log('\n=== Pagination ===');
const pages = paginate(['A','B','C','D','E','F','G'], 3);
console.log('Page 1:', pages.next().value);
console.log('Page 2:', pages.next().value);
console.log('Page 3:', pages.next().value);Remember: a generator is the lab's conveyor belt, which delivers the next sample only when you reach out for it.
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which method must an object implement to be iterable (usable in for...of)?
2. How do you mark a function as a generator in JavaScript?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Click in order
Arrange the elements to create an async generator:
- Code editor
Implement [Symbol.iterator], an ID generator, and a pagination generator
- Vertical ordering
Arrange the steps of an iterator from the first next() call to completion:
- Click in order
Arrange the elements to create a simple generator:
- Code editor
Implement Fibonacci, yield* for composing generators, and a range(start, end, step) generator
- Vertical ordering
Arrange the elements of an iterator protocol implementation:
- Click in order
Arrange the elements of a generator with multiple yields:
Tasks after this lesson
- Click in orderAsync Generator Syntax
- QuestionIterable Protocol
- QuestionGenerator Function
- Coding taskPractice: Custom Iterator and Generator
- Put in orderObject Returned by next()
- Click in orderBuilding a Generator
- Coding taskPractice: yield* and Advanced Generators
- Questionfor...of vs for...in
- Put in orderIterator Protocol Syntax
- Click in orderyield Elements in a Generator