Whenever you have a question about ES6 generator, or a question strongly related to it, this is an appropriate tag.
Javascript generators are special function
s, which can be paused with the keyword yield
, which can be used to establish a 2-way communication between the generator and its caller.
Generator function declaration:
function *myGenerator(params) {/*...*/}
The star (*
) character before myGenerator()
above means that the function is a generator.
Generator function expression:
const myGenerator = function *(params) {/*...*/}
When using a function expression to create a generator the star (*
) between the function
keyword and the opening parenthesis above means that the function is generator.
We can assign the function
to a variable, but this is not starting the function
, anything passed as a parameter will be ignored:
var myExecutor = myGenerator();
myExecutor
is an iterable, which steps through the generator, stopping at each yield
.
myExecutor.it(someparams)
resumes the generator from the last yield where it stopped previously (or the start of the function
) and it executes the function
until the next yield
, or, in the case when there is no next yield
, until the end of the function
.