The Yield Function
developer.mozilla.org·12w·
Discuss: Hacker News

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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help