The Yield Function
developer.mozilla.org·5h·
Discuss: Hacker News
Flag this post

Try it

function* foo(index) {
while (index < 2) {
yield index;
index++;
}
}

const iterator = foo(0);

console.log(iterator.next().value);
// Expected output: 0

console.log(iterator.next().value);
// Expected output: 1

Syntax

yield
yield expression

Parameters

expression Optional

The value to yield from the generator function via the iterator protocol. If omitted, undefined is yielded.

Return value

Returns the optional value passed to the generator’s next() method to resume its execution.

Note: This means next() is asymmetric: it always sends a value to the …

Similar Posts

Loading similar posts...