Generators and Iterators

Generators are function execution contexts that can be paused and resumed. When you call a normal function, it will likely return a value; the function fully executes, then terminates. A Generator function will yield a value then stop but the function context of a Generator is not disposed of (as it is with normal functions). You can re-enter the Generator at a later point in time and pick up further results.

An example might help:

function* threeThings() {    yield 'one';    yield 'two';    yield 'three';}let tt = threeThings();console.log(tt); // {} console.log(tt.next()); // { value: 'one', done: false }console.log(tt.next()); // { value: 'two', done: false }console.log(tt.next()); // { value: 'three', done: false }console ...

Get Mastering Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.