liveBook · Manning

Yield

As explained in Secrets of the JavaScript Ninja, Second Edition

An expression in a JavaScript generator that yields a value and evaluates to a value supplied when execution resumes.

A normal function runs until it returns. A generator can pause partway through: `yield` produces a value, suspends execution without blocking the main thread, and leaves the generator ready to continue when asked. Declare a generator with `function*`; calling it creates an iterator rather than running its body immediately. section 6.2 section 6.2.1

Call the iterator’s `next()` method to run to the next `yield`. Each call returns an object with `value` and `done` properties. A `for-of` loop can request yielded values for you instead of making those calls yourself. section 6.2.1

A value goes out; another comes back

`function* exchange() { const reply = yield "Ready"; yield "Received: " + reply; }` Calling `exchange()` creates an iterator. Its first `next()` yields `"Ready"`. Calling `next("yes")` resumes execution, makes the suspended yield expression evaluate to `"yes"`, and yields `"Received: yes"`. section 6.2.3

Two useful variations:

  • Use `yield*` to yield values from another generator as part of the sequence. section 6.2.1
  • Use the iterator’s `throw()` method to send an exception into a suspended generator, where it can be caught. section 6.2.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains yield through iterator results and resumed expressions, then shows delegation and injected exceptions; stops short of runtime internals.
Examples
Uses weapon sequences, generator-to-generator delegation, and a two-way exchange through next().
What it assumes
Assumes familiarity with JavaScript functions and calls while introducing generator declaration, iterators, and yield from their first use.
What you can do after
Build it from scratch by yielding a sequence and receiving a value when the generator resumes.
How it connects
Connects yield to generator functions, iterators, for-of loops, yield* delegation, and asynchronous work with promises.

Also explained in

Compare how 10 books explain yield →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.