genFunc() doesn’t execute the body of the function.
Instead, it returns generator object which can be used to control the execution of the generator.
genObj.next() will start executing the function
it will execute till it reaches
We can think of
return for now.
It is similar to C#
genObj.next() again will resume
the further execution after yield.
Here’s how the execution looks like:
yield is not allowed inside non-generator functions.
That is, yielding in callbacks doesn’t work.
We can also pass the data to generator function via
We can retrieve the yielded values via the generator object
We can have multiple
yield in the function
as shown in the above example.
Once, the execution of the generator function is completed,
further calls to
genObj.next() will have no effect.
We highly recommend Exploring ES6 by Dr. Axel Rauschmayer to go deeper on this topic.