← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

JavaScript Generators

javascript ES2015 Advanced
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints note automated=no, and while ESLint and TypeScript are listed tools, there is no standard rule that catches misuse of generators (e.g., building a full array instead of using a lazy generator, or incorrectly calling .next()). These mistakes are silent in normal execution — memory bloat only surfaces under load, and the wrong mental model (treating generators like arrays) produces code that runs but wastes resources, requiring careful review or profiling to catch.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix indicates replacing eager array construction with generator functions using function* and yield. This is typically a localised refactor within one component — replacing a data-producing function and updating call sites to use for...of — not a single one-liner, but not a cross-cutting change either.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Generators apply to web and CLI contexts but are a local choice: adopting them in one module or utility does not impose a systematic cost on the rest of the codebase. Callers consume via for...of or .next(), which is standard iterable protocol. The burden is confined to the specific code paths using them.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field explicitly states developers believe generators are just a different way to write arrays, when they are fundamentally lazy and potentially infinite. Additionally, common_mistakes highlight that calling the generator function returns a generator object (not the first value), and the final .next() returns {done: true} — both contradict intuitions from similar constructs in other languages or from array/promise patterns, making this a serious cognitive trap.

About DEBT scoring →

Also Known As

generator function function* yield

TL;DR

Functions that can pause execution and yield values on demand — enabling lazy sequences, infinite iterables, and cooperative async control flow.

Explanation

A generator function (function*) returns a generator object. Calling next() runs the function until the next yield, returns the yielded value, then pauses. Generators are lazy — they compute values on demand rather than all at once. They underlie async/await (which was originally implemented as a syntax transform over generators + Promises). Use cases: infinite sequences, paginated API iteration, streaming data processing, and coroutines.

Diagram

flowchart TD
    subgraph Generator_Function
        GF[function* generator]
        GF -->|call| ITER[Returns iterator<br/>not executed yet]
        ITER -->|next call| Y1[Run until yield<br/>return value + suspended]
        Y1 -->|next call| Y2[Resume from yield<br/>next value]
        Y2 -->|next call| DONE[done: true<br/>value: undefined]
    end
    subgraph Use_Cases
        LAZY[Lazy sequences<br/>infinite lists]
        ASYNC2[Async orchestration<br/>co-routines]
        ITER2[Custom iterators<br/>Symbol.iterator]
    end
style GF fill:#6e40c9,color:#fff
style ITER fill:#1f6feb,color:#fff
style DONE fill:#d29922,color:#fff

Common Misconception

Generators are just a different way to write arrays — generators are lazy and can be infinite; arrays materialise all values in memory at once.

Why It Matters

Processing a 10 million row dataset with a generator uses O(1) memory; loading it into an array uses O(n) — for large streams, generators are the only viable approach.

Common Mistakes

  • Calling the generator function itself — it returns a generator object, not the first value; call .next() to start.
  • Not handling generator return values — the final .next() returns {value: returnValue, done: true}.
  • Using generators where async/await is clearer — generators are the right tool for lazy sequences, not general async code.
  • Not using yield* to delegate to another iterable — manually iterating inside a generator is verbose.

Code Examples

✗ Vulnerable
// Loads all pages into memory before processing:
async function getAllUsers() {
    const users = [];
    let page = 1;
    while (true) {
        const batch = await fetchPage(page++);
        if (!batch.length) break;
        users.push(...batch); // Could be millions of users in memory
    }
    return users;
}
✓ Fixed
// Generator — processes one page at a time, O(1) memory:
async function* paginatedUsers() {
    let page = 1;
    while (true) {
        const batch = await fetchPage(page++);
        if (!batch.length) return;
        yield* batch; // Yield each user one at a time
    }
}

// Caller:
for await (const user of paginatedUsers()) {
    await processUser(user); // Never loads all users at once
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 6 Unknown AI 3 Ahrefs 2 SEMrush 2 Majestic 1 ChatGPT 1 Google 1
crawler 23 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Use generator functions (function*) with yield for lazy sequences and async iteration — they power the for...of loop and are the foundation of async iterators in JavaScript
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
Array built entirely before iteration when lazy generator would save memory; no async generator for streaming data
Auto-detectable: ✗ No eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant