JavaScriptFundamental (ES6), part 2
Delegate to another generator with yield* in JavaScript
5 Delegate with yield*, and catch what it returns
fairUse yield* to splice one generator's output into another. Capture the delegated generator's return value, delegate to a plain array and a string as well, and show that a value sent with next() reaches all the way through.
Stuck? Three hints, smallest first
- 1.
yield* inner()yields everythinginneryields, one at a time, inline. - 2. The expression
yield* inner()evaluates toinner’s return value. - 3.
yield*takes any iterable, and it forwardsnext(),throw()andreturn()to the delegate.
function* inner() {
yield 'a';
yield 'b';
return 'inner result';
}
function* outer() {
const handedBack = yield* inner(); // the inner RETURN lands here
yield handedBack;
yield* [1, 2]; // any iterable
yield* 'hi'; // a string is one too
}
// The hand-rolled relay yields the same values and loses the return value.
function* relay() {
for (const v of inner()) yield v;
}
function* deep(n) {
if (n === 0) return;
yield n;
yield* deep(n - 1);
}
// next() is forwarded through the delegation, not swallowed by it.
function* echo() { const got = yield 'ready'; return 'got ' + got; }
function* wrapper() { const r = yield* echo(); yield r; }
const w = wrapper();
w.next();
const passedThrough = w.next('cash').value;
console.log('outer :', [...outer()]);
console.log('relay :', [...relay()], '<- the return value is gone');
console.log('deep :', [...deep(3)]);
console.log('sent through the delegation:', passedThrough);
Output
7 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 7 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('delegation yields the inner values inline', [...outer()].slice(0, 2), ['a', 'b']);
test('the yield* expression is the inner return value', [...outer()][2], 'inner result');
test('and any iterable can be delegated to', [...outer()], ['a', 'b', 'inner result', 1, 2, 'h', 'i']);
test('a for...of relay yields the same values', [...relay()], ['a', 'b']);
test('but throws the return value away', [...relay()].length, 2);
test('yield* recurses happily', [...deep(3)], [3, 2, 1]);
test('a value sent with next() reaches the delegated generator', passedThrough, 'got cash');
yield* is not sugar for a for...of relay, and the difference is the return value. The relay reads the delegate through the iterator protocol, which stops at done: true and never looks at the value beside it. yield* is specified to pick that value up and become it.
The other difference is transparency. yield* forwards next(v), throw(e) and return(v) down to the delegate, so a nested stack of generators behaves like one generator from the outside. A hand-written relay forwards none of that: next('cash') would resume the relay's own yield and the inner generator would never see it.
Recursion plus yield* is the idiomatic tree walk, and exercise 10 does it properly. Be aware that it costs a frame per level per value, so a very deep structure is better flattened with an explicit stack.