Skip to content
Concept 67 // Module 07: Collections, Iteration & Modules

Generator Delegation: yield* & Communication

Module 07: Collections, Iteration & Modules•Concept 67 of 100• 5 min read

Generator Delegation: yield* & Communication

Advanced Generators leverage **two-way communication** with `it.next(value)` to pass data into the generator, and `yield*` to delegate iteration to another generator or iterable.

The Core Concept

Generators provided the foundational architecture for async/await before native browser support existed (e.g. libraries like `co` and Redux Saga).

Syntax Reference
function* delegateGen() {
  yield* [1, 2];
  const input = yield "Send me something";
  yield `You said: ${input}`;
}

Live Interactive Studio

Values passed to `next(val)` become the return value of the paused `yield` expression inside the generator.
JavaScript Editor
Loading...
Console Output
Waiting for output...

Key Code Patterns in Action

01

Two-Way Data Transfer via next()

function* dialog() {
  const name = yield "What is your name?";
  const role = yield `Welcome, ${name}! What is your role?`;
  return `Profile created: ${name} the ${role}`;
}

const bot = dialog();
console.log(bot.next().value);       // "What is your name?"
console.log(bot.next("Subhajit").value); // "Welcome, Subhajit! What is your role?"
console.log(bot.next("Architect").value); // "Profile created: Subhajit the Architect"
Values passed to `next(val)` become the return value of the paused `yield` expression inside the generator.
02

Recursive Tree Flattening with yield*

function* walkTree(node) {
  yield node.val;
  if (node.children) {
    for (const child of node.children) {
      yield* walkTree(child); // Delegating to child generator
    }
  }
}

const tree = { val: 1, children: [{ val: 2 }, { val: 3, children: [{ val: 4 }] }] };
console.log([...walkTree(tree)]); // [1, 2, 3, 4]
`yield*` transparently yields all values from the sub-generator without manual loop unwrapping.
03

1. Core Usage: Generator Delegation

// Core syntax and usage for Generator Delegation: yield* & Communication
const feature = 'generator-delegation-yield-star-communication';
console.log('Concept 67:', feature);
console.log('Active:', true);
Illustrates the fundamental invocation and syntax structure for Generator Delegation: yield* & Communication.
04

2. Defensive Handling & Edge Case

// Edge-case defensive handling
function handleSafe(input) {
  if (input === null || input === undefined) return 'safe-fallback';
  return String(input).trim();
}
console.log(handleSafe('generator-delegation-yield-star-communication'));
console.log(handleSafe(null));
Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Generator Delegation: yield* & Communication.
05

3. Clean Architecture & Production Pattern

// Production-grade pattern for Generator Delegation: yield* & Communication
const settings = Object.freeze({
  id: 'generator-delegation-yield-star-communication',
  ready: true,
  timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);
Demonstrates clean, immutable production code avoiding unintended state mutations.

Real-World Engineering Takeaways

Redux Saga: Managing complex async side effects and action coordination via sagas.
Infinite Lazy Sequences: Generating prime numbers, IDs, or paginated requests on demand.
State Machines: Implementing sequential step-by-step game loop or wizard workflows.

Under the Hood: Engine & Memory Mechanics

How JavaScript Executes This
When a generator yields, its execution stack frame is suspended and preserved as a heap-allocated GeneratorObject containing its register state, local variables, and program counter.

Quick Knowledge Check

Test your understanding on these 10 questions (one by one):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of Generator Delegation: yield* & Communication in JavaScript?

// Concept 67: Generator Delegation: yield* & Communication
// Understanding the core mental model
1 / 10