generators - mmedrano9438/peripheral-brain GitHub Wiki

The ES6 generator is a new type of function that can be paused in the middle or many times in the middle and resumed later.

For example the, (*) yield operator, will suspend the function execution in the following example: function *myfunction() { }

The next() method will resume the execution of the generator function when it receives an argument, replacing the yielded expression where the execution was paused with the argument from the next() method. Objects returned by the next() method always have two properties.

value-The yielded value is the value. done- The completed state of a function can be expressed as a Boolean value true. Otherwise, it yields false.

function* generator() { yield 1; yield 2; yield 3; } let obj = generator(); console.log(obj.next()); console.log(obj.next()); console.log(obj.next()); console.log(obj.next());

OUTPUT: { done: false, value: 1 } { done: false, value: 2 } { done: false, value: 3 } { done: true, value: undefined }