DEV Community

Cover image for The Secret Life of JavaScript: The Generator

The Secret Life of JavaScript: The Generator

Aaron Rose on February 01, 2026

How to pause, resume, and control the flow of execution. Timothy was staring at a frozen screen. He forced a reload, but he looked defeated. ...
Collapse
 
charanpool profile image
Charan Koppuravuri

🔥 Spot-on teaching. Generators = controlled execution suspension.

Real production use (from building LLM frameworks):


js
function* createRecoveryPointIds() {
  let timestamp = Date.now();
  let sequence = 0;
  while (true) {
    yield `rpe_${timestamp}_${sequence++}`;
    if (sequence > 999) {
      timestamp = Date.now();
      sequence = 0;
    }
  }
}

// Usage in ETL pipeline
const idGen = createRecoveryPointIds();
for (const record of recoveryPoints) {
  record.id = idGen.next().value; // Non-blocking ID stream
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

✨💯❤️

Collapse
 
bhavin-allinonetools profile image
Bhavin Sheth

Really enjoyed this — the way you explain generators as “pausing time” makes a tricky concept feel simple and practical. The ID example is super relatable and the two-way communication part clicked instantly for me.

Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

❤🙏

Collapse
 
m-ahmad-usman profile image
Muhammad Ahmad

In the start you mentioned to create a system to creat unique IDs forever but this system will not always generate unique IDs. It will initialize from start once the server restarts or program is being executed again.

Collapse
 
peacebinflow profile image
PEACEBINFLOW

This was a really enjoyable read.

I like how you framed generators as a shift in how time works rather than just another JS feature. That “run-to-completion vs pause and resume” distinction is one of those things that quietly changes how you think about programs once it clicks.

The ID example is perfect too — it’s simple, but it exposes a real mental model gap. A lot of people try to solve problems like this by reaching for more state or globals, when the real issue is that normal functions just don’t give you control over execution flow.

The “conversation with the function” line is especially good. That’s exactly how generators feel once you get comfortable with them — not just yielding values, but passing intent back in. It makes things like lazy iteration and controlled infinite sequences feel way less magical and way more deliberate.

Also appreciate that you showed for...of and the iterator protocol instead of leaving generators in the “weird syntax corner.” That’s usually the moment where people realize this isn’t a niche trick, it’s a core language concept.

Solid explanation overall. Definitely one of those posts that makes a feature stick instead of just explaining it.

Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

❤️🙏

Collapse
 
arichy profile image
Arc

I've written JS for 9 years, wrote sync generator only less than 5 times.

Collapse
 
martiserra99 profile image
Martí Serra Molina

Really liked this article! I knew about the iterator pattern but not using this approach! I will keep this in mind in case I ever need to create an iterator.

Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

❤🙏✨

Collapse
 
benjamin_nguyen_8ca6ff360 profile image
Benjamin Nguyen

Great explaining, Aaron! I enjoy you and other people article. They are so easy to follow and understand.

Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

🙏❤️✨