Simple Generator Function

ES6

A generator function is used for defining an iterator that can maintain its own state. The generated iterator can also be paused and resumed.

function *Count() {
  yield 1;
  yield 2;
  yield 3;
  return 4;
}

The generator function is defined with an asterisk (*) and has one or more yield expressions.

const count = new Count();

We invoke the Count() generator function to the newcount constant.

count.next(); // { value: 1, done: false}
count.next(); // { value: 2, done: false}
count.next(); // { value: 3, done: false}
count.next(); // { value: 4, done: true}

count is now iterable. We can step through each iteration when we call next().

The returned object has a value, which is yielded by the generator function, and a done boolean, which indicates if we have complete all yields.

for (let v of Count()) {
    console.log(v); //1,2,3,4
}

We can iterate through the count with the for-of loop.

[...count] //[1,2,3]

We can convert the iterator in to a simple array using the spread operator.

results matching ""

    No results matching ""