DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. JavaScript Generators and Iterators: Beyond the Basics
XLinkedInReddit
MediumFrontend Engineering

JavaScript Generators and Iterators: Beyond the Basics

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • The Iterator Protocol
  • Generator Functions
  • Practical Use Cases
  • 1. Lazy Pagination
  • 2. Cancellable Async Flows
  • 3. Tree Traversal
  • Generator + Async = Power

Generators are one of the most underused features in JavaScript. At Google, we use them for lazy evaluation, pagination, and complex async flows.

The Iterator Protocol

Any object with a next() method returning { value, done } is an iterator. Any object with [Symbol.iterator]() returning an iterator is iterable.

const range = {
  *[Symbol.iterator]() {
    for (let i = this.start; i <= this.end; i++) yield i;
  },
  start: 1,
  end: 5
};
console.log([...range]); // [1, 2, 3, 4, 5]

Generator Functions

Generators are functions that can pause and resume. The yield keyword pauses execution and returns a value.

function* fibonacci() {
  let [a, b] = [0, 1];
  while (true) {
    yield a;
    [a, b] = [b, a + b];
  }
}
const fib = fibonacci();
fib.next().value; // 0
fib.next().value; // 1
fib.next().value; // 1

Practical Use Cases

1. Lazy Pagination

async function* fetchPages(url) {
  let page = 1;
  while (true) {
    const res = await fetch(`${url}?page=${page}`);
    const data = await res.json();
    if (data.length === 0) return;
    yield data;
    page++;
  }
}
for await (const page of fetchPages("/api/users")) {
  renderUsers(page);
}

2. Cancellable Async Flows

Unlike Promises, generators can be cancelled by simply not calling next() again. This is the foundation of libraries like Redux-Saga.

3. Tree Traversal

function* traverse(node) {
  yield node.value;
  for (const child of node.children) {
    yield* traverse(child);
  }
}

Generator + Async = Power

Async generators (async function*) combine the best of both worlds — lazy evaluation with async data sources. This is how Node.js streams work under the hood.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Iterator Protocol
  • Generator Functions
  • Practical Use Cases
  • 1. Lazy Pagination
  • 2. Cancellable Async Flows
  • 3. Tree Traversal
  • Generator + Async = Power

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.