Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Use Generators for Iterable Functions

- 19.07.26 - ErcanOPAK

🔄 Generators = Iterable Functions

Normal functions run completely. Generators pause and resume. Infinite lists, async flows, custom iteration.

📝 Generator Basics

// Generator function
function* numberGenerator() {
    yield 1;
    yield 2;
    yield 3;
}

// Using generator
const gen = numberGenerator();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: 3, done: false }
console.log(gen.next()); // { value: undefined, done: true }

// For-of loop
for (const num of numberGenerator()) {
    console.log(num); // 1, 2, 3
}

// Spread operator
const numbers = [...numberGenerator()]; // [1, 2, 3]

// Infinite sequence
function* infiniteNumbers() {
    let i = 0;
    while (true) {
        yield i++;
    }
}

const infinite = infiniteNumbers();
console.log(infinite.next().value); // 0
console.log(infinite.next().value); // 1
console.log(infinite.next().value); // 2

// Fibonacci sequence
function* fibonacci() {
    let a = 0, b = 1;
    while (true) {
        yield a;
        [a, b] = [b, a + b];
    }
}

const fib = fibonacci();
console.log(fib.next().value); // 0
console.log(fib.next().value); // 1
console.log(fib.next().value); // 1
console.log(fib.next().value); // 2
console.log(fib.next().value); // 3

🎯 Advanced Generators

// Yield with return value
function* generatorWithReturn() {
    yield 1;
    yield 2;
    return 3; // Ends generator
}

const gen2 = generatorWithReturn();
console.log(gen2.next()); // { value: 1, done: false }
console.log(gen2.next()); // { value: 2, done: false }
console.log(gen2.next()); // { value: 3, done: true }

// Yield with next parameter
function* interactiveGenerator() {
    const input1 = yield 'First input?';
    const input2 = yield 'Second input?';
    return `You said: ${input1} and ${input2}`;
}

const interactive = interactiveGenerator();
console.log(interactive.next().value); // 'First input?'
console.log(interactive.next('Hello').value); // 'Second input?'
console.log(interactive.next('World').value); // 'You said: Hello and World'

// Async generators
async function* asyncGenerator() {
    yield await Promise.resolve(1);
    yield await Promise.resolve(2);
    yield await Promise.resolve(3);
}

for await (const value of asyncGenerator()) {
    console.log(value); // 1, 2, 3
}

// Generator Use Cases
- Infinite sequences
- Lazy evaluation
- State machines
- Async flows
- Data pagination

💡 Generator Tips

  • Use for infinite sequences
  • Use for lazy evaluation
  • Use for state machines
  • Use for async flows
  • Use for data pagination

“Generators enable lazy iteration. Infinite lists, state machines. Essential for advanced JavaScript.”

— JavaScript Expert

Related posts:

JavaScript: Fix a Proxy-Wrapped Object Losing Its Custom Methods After It's Passed Through JSON.stri...

JavaScript: Fix a Proxy-Wrapped Object Losing Its Custom Methods After It's Passed Through JSON.stri...

JavaScript: Fix a setTimeout Delay That Fires Immediately Because a Non-Numeric String Was Passed In...

JavaScript: Fix a setTimeout Delay That Fires Immediately Because a Non-Numeric String Was Passed In...

Why forEach Can Be Slower Than for (And When It Matters)

Why forEach Can Be Slower Than for (And When It Matters)

Views: 11

Post navigation

HTML: Build Reusable Web Components
Ajax: Use WebSockets for Real-Time Communication

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com