- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Generator Communication
JavaScript Generators & Iterators
JavaScript Generator Communication
Values can be sent into a running generator through
next, not just read out of it.
A yield expression itself has a value, supplied by the next next() call.
This turns a generator into a genuine two-way conversation.
Example
Example
javascript
function* greeter() {
const name = yield "What is your name?";
yield "Hello " + name;
}
const g = greeter();
console.log(g.next().value);
console.log(g.next("Ada").value);