liveBook · Manning

Next

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A method on a generator object that advances execution until the next yield or completion and may supply a value back into the generator.

Want to get one value from a generator without running it all at once? Calling `next()` on its generator object starts or resumes execution. The generator runs until it reaches a `yield` or finishes, then `next()` returns an object with `value` and `done` properties. section 18.2

Start, pause, and send a value back

```js function* exchange(initial) { console.log('started'); const reply = yield initial; yield reply; return 'finished'; } const generator = exchange('a'); // Nothing logged yet. generator.next('ignored'); // Logs 'started'; { value: 'a', done: false } generator.next('b'); // { value: 'b', done: false } generator.next(); // { value: 'finished', done: true } ``` The first `next()` starts the halted generator; its argument is ignored because execution has not reached a `yield` to receive it. Use a generator-function parameter for an initial value. The next call supplies `'b'` as the result of the paused `yield`. On completion, `done` is `true`; a returned value appears in `value`, while simply reaching the end produces `undefined`. section 18.2

`next()` also defines an iterator’s basic job: produce the next value and signal when iteration is done. An object’s `Symbol.iterator` method can return an iterator with a `next()` method. Spread and `for..of` call that method repeatedly, stopping when `done` is `true`. A generator can serve as that iterator, avoiding a hand-written `next()` method. Don’t spread an infinite iterator: it never reports completion. section 23.4 section 23.5

Put the feedback loop to work

A simulation can call `next()` to obtain a contestant, then pass previous outcomes into a later `next()` call. The generator receives those outcomes at its paused `yield` and can use them to shape its next contestant. For practice, make an infinite date generator whose first `next()` yields today and whose later calls yield successive days. section 19.1 section 18.4

A method named `next()` is not always an iterator method. A sprite’s `next()` hook, for example, runs before each frame and returns the sprite itself—not a `{ value, done }` result. section 29.3

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through initial calls, yield feedback, completion results, and the iterator return contract without descending into language internals.
Examples
Traces yielded letters, constructs a prime-number iterator, and feeds previous outcomes into a simulation generator.
What it assumes
Assumes JavaScript function and object syntax while introducing generator lifecycle and iterator requirements from their basics.
What you can do after
Build it from scratch as a generator or iterator that yields successive values and handles completion or feedback.
How it connects
Connects generator functions and yield to the iterator protocol, iterables, spread, and for..of.

Also explained in

Compare how 5 books explain next →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.