the value that was passed in by
next
. Here’s what it looks like when we run this gener‐
ator through to completion:
const
it
=
interrogate
();
it
.
next
();
// { value: "What is your name?", done: false }
it
.
next
(
'Ethan'
);
// { value: "What is your favorite color?", done: false }
it
.
next
(
'orange'
);
// { value: "Ethan's favorite color is orange.", done: true }
shows the sequence of events as this generator is run.
Figure 12-1. Generator example
This example demonstrates that generators are quite powerful, allowing the execution
of functions to be controlled by the caller. Also, because the caller can pass informa‐
tion into the generator, the generator can even modify its own behavior based on
what information is passed in.
You can’t create a generator with arrow notation; you have to use
function*
.
Generators | 181