Generators Receive Values Through next()
generator .next(value) passes input into the paused yield expression, and the generator's return value surfaces as the final done result object.
Code
function* echoPipeline() {
// yield both produces a value and receives the argument of the next .next()
let received = yield 'ready';
while (received !== 'done') {
received = yield 'echo:' + received;
}
return 'finished';
}
const it = echoPipeline();
// The first .next() carries no input: it runs up to the first yield
console.log(it.next().value);
console.log(it.next('hello').value);
console.log(it.next('world').value);
// Passing 'done' exits the loop; the return value lands in .value with done true
console.log(it.next('done'));
console.log(it.next().done);Output
Captured from a sandboxed run of this snippet in the build pipeline.
ready
echo:hello
echo:world
{ value: 'finished', done: true }
trueHow it works
The cycling part of this snippet is while (received !== 'done').
Module-level bindings hold the data — it — with no function scope in between.
What to watch for
- Nothing to vet in a supply-chain review — the file pulls in no packages.
Core lines
received = yield 'echo:' + received;
let received = yield 'ready';
return 'finished';
Interactive Tools for This Topic
FAQ
- Does this require external packages?
- No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. Its state lives in module-level bindings: `it`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.