return in a generator function is special
the returned value is an IteratorResult (just like any value returned via yield)
returns an IteratorResult (an object with the properties value and done)
the property value is the returned value
the property done is true
NOTE: yield does not return done=true but done=false!
a missing return returns {value: undefined, done: true}
mixing return and yield
is possible
the mix behaves different to two yields
two yields returning values
return a yielded value by "chaining" return and yield
Links
Description of the IteratorResult interface.
Describes how a return statement behaves inside a generator.
Required Knowledge
- `const` declaration (Block scope)
 - object (Destructuring)
 - `Array.from()` (Array API)
 - protocol (Iterator)
 - creation (Generator)
 - iterator (Generator)
 - yield expressions (Generator)
 - send value to a generator (Generator)
 
Related Katas
Generator
- creation
 - iterator
 - yield expressions
 - send value to a generator
 - send function to a generator
 returninside a generator function- send return to a generator
 
Difficulty Level
ADVANCED
First Published
25 September 2015
Stats
9 tests to solve