Sub topic 12
Sending Values In: next(value), throw(), and return()
This chapter assumes you know how generators pause at yield and resume on next(), as covered in the chapter on generator execution. So far, values have flowed only out of a generator. A generator object has three methods that also send something in: next(value), throw(error), and return(value).
next(value): answering a yield
A yield expression produces a value for the caller, and it also evaluates to whatever the caller passes to the next next() call. The generator pauses at yield. When next(5) resumes it, the paused yield expression becomes 5.
function* runningTotal() {
let total = 0;
while (true) {
const n = yield total;
total += n;
}
}
const acc = runningTotal();
console.log(acc.next()); // { value: 0, done: false } (starts the generator)
console.log(acc.next(5)); // { value: 5, done: false }
console.log(acc.next(10)); // { value: 15, done: false }
console.log(acc.return(99)); // { value: 99, done: true }The first next() only runs the body up to the first yield. No yield is waiting yet, so any argument you pass to that first call is discarded. This "priming" call is the most common source of confusion with two-way generators.
throw(error): raising an error at the yield
gen.throw(err) resumes the generator, but it throws err at the paused yield instead of returning a value there. If the generator catches the error, it carries on and throw() returns the next yielded result. If the generator doesn't catch it, the error propagates out of throw() and the generator finishes.
function* worker() {
while (true) {
try {
const job = yield 'ready';
console.log('got', job);
} catch (err) {
console.log('caught', err.message);
}
}
}
const w = worker();
w.next(); // start: { value: 'ready', done: false }
w.next('job A'); // logs: got job A
console.log(w.throw(new Error('bad input'))); // logs: caught bad input
// then: { value: 'ready', done: false }
console.log(w.return('stopped')); // { value: 'stopped', done: true }return(value): finishing early
gen.return(value) acts as if a return value; statement ran at the paused yield. You get back { value, done: true }, and every later next() returns { value: undefined, done: true }. for...of calls return() for you when you break out of a loop. The next chapter shows how finally blocks run during this forced return.
Summary of the three methods
| Method | Effect at the paused `yield` | Typical result |
|---|---|---|
| `next(v)` | `yield` evaluates to `v` | Next `{ value, done: false }` |
| `throw(e)` | `yield` throws `e` | Next yield if caught, else the error propagates |
| `return(v)` | Behaves like `return v` | `{ value: v, done: true }` |
Exercise
Given function* echo() { const a = yield 'first'; const b = yield a; return b; } and const g = echo();, what do g.next('x'), g.next('y'), and g.next('z') return, in order?
Show answer
{ value: 'first', done: false } (the 'x' is discarded because no yield was waiting), then { value: 'y', done: false } (a is 'y'), then { value: 'z', done: true } (b is 'z' and it is returned).
Exercise
In the worker example, what happens if you remove the try...catch and call w.throw(new Error('bad input')) after starting the generator?
Show answer
The error goes uncaught inside the generator, so it propagates out of the w.throw() call to the caller, and the generator finishes. Later next() calls return { value: undefined, done: true }.