ES6 – Iterating over a generator function

Hi, in this post we are going to see how we can iterate over a generator function.

As we know the generator function will returns a generator object and this object is iterable, so in this case we can use the new loop of ES6 “for…of” that is used to iterate over the values of an iterable object.

function* my_first_generator_function() {
  yield 100;
  yield 200;
  yield 300;
  yield 400;
  yield 500;
}

for (let value of my_first_generator_function()) {
  console.log("value --->", value);
}

Result:

Nice, if you want to know more about “for…of” then read my post:

By Cristina Rojas.