Class: Generator

Generator(func, …args)

Generators are the Mildew implementation of coroutines. They are defined by adding an asterisk (*) after the function keyword in a function declaration or definition, and created by calling that same function. They can also be created by calling the Generator constructor with a regular Function and argument list. Generator functions created with the Generator constructor may not use the yield expression directly but may call a function called __yield__. The yield keyword can only be used directly in Generator functions defined with the asterisk (*) notation and not in normal functions called by a Generator.

For-of loops that declare one variable may iterate over a Generator.

It is an error to run a Generator whose last iteration return value contained a done=true field. If an error occurs and is not caught inside the Generator's function, it will propagate to the caller of the Generator method.

Finished Generator objects should be set to undefined or any other value so as not to waste memory because they use their own copy of the virtual machine to avoid stack collisions.

Constructor

new Generator(func, …args)

Creates a new Generator from a given Function and its arguments. If a specific "this" object is desired to be used with func, func=func.bind(someThisObject) should be set before calling this constructor.
Parameters:
Name Type Attributes Description
func function A Function that contains yields.
args * <repeatable>
The arguments that will be passed to the Function

Members

name

Gets the name of this Generator object. This is set to the Function's name upon instantiation.

returnValue

This property has nothing to do with the return method. In Mildew, deviating from other languages, Generator functions may have return statements and a return value. This property extracts the return value once the Generator is finished, otherwise it returns undefined.

Methods

next(value) → {Object}

Run the generator one time and get the next yield.
Parameters:
Name Type Description
value * If yield is used as the right hand side of an assignment, this value will be the result.
Returns:
An Object containing the following fields: "value", which is the last yielded value, or could be undefined if the Generator is done, and "done", a boolean of true or false indicating whether or not the Generator can be run any further.
Type
Object

return(value) → {Object}

Runs the Generator to completion and returns the value that was sent.
Parameters:
Name Type Description
value * The value to serve as the "value" field in the return value Object.
Returns:
An object containing a "value" field set to the parameter, and "done" which is always set to true.
Type
Object

throw(error) → {Object}

Similar to next(), but causes the Generator to throw an error on its next yield. If this error is not caught inside the Generator function it will propagate to the call site of throw. It is an error to call throw on a finished Generator.
Parameters:
Name Type Description
error * Any value to serve as the error being thrown.
Returns:
An object containing a possible "value" field and a "done" field that is either true or false.
Type
Object