Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
JavaScript

JavaScript: Fix a Generator Function Leaking Across Iterations

- 30.11.26 - ErcanOPAK

⚡ Fix a Generator Function Leaking Across Iterations

A generator function’s local variables persist on its own suspended execution context between yield calls by design, which is exactly what makes generators useful — but a generator that captures a mutable object from its outer scope and yields a reference to that same object on every iteration hands every consumer a view into state that keeps changing underneath them after each .next() call, instead of a fresh snapshot per iteration.

🔎 The Problem

function* readings(sensor) {
  const buffer = { value: null };
  while (true) {
    buffer.value = sensor.read();
    yield buffer; // same object reference every time
  }
}
const gen = readings(sensor);
const a = gen.next().value;
const b = gen.next().value;
console.log(a.value === b.value); // true - "a" silently changed too

✅ Fix: Yield a Fresh Value or Copy Instead of a Shared Reference

  • Yield a new object literal on each iteration (yield { value: sensor.read() }) instead of mutating and re-yielding the same captured object, so every consumer gets an independent snapshot.
  • If the buffer object is kept around deliberately for performance, document that loudly and have consumers read the value out immediately rather than holding onto the yielded reference across iterations.
  • Add a quick regression test that calls .next() twice, stores both results, and asserts they’re independent — this bug is invisible in a quick console log but obvious the moment two results are compared.

A generator’s job is to pause and resume state, not to hand out the same mutable object dressed up as a new result each time.

— JavaScript Tip

Related posts:

Ajax Requests Succeed but Data Is Stale

Ajax Requests Succeed but Data Is Stale

JavaScript: Use Proxy to Create Reactive Objects Without Framework

JavaScript: Use Proxy to Create Reactive Objects Without Framework

JavaScript Promise.all and Promise.race: Master Async Control

JavaScript Promise.all and Promise.race: Master Async Control

Views: 3
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

C#: Fix a Volatile Field Reordered Around a Non-Volatile Read
The AI Prompt That Hunts Down a Leaked Event Subscription

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

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

Social

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