Yield a sequence from a generator function.

Generator Basics

generator_basics.js
const count = 3;
const values = [];

function* numbers(limit) {
  let current = 1;
  while (current <= limit) {
    yield current;
    current = current + 1;
  }
}

for (const value of numbers(count)) {
  values.push(value);
}

const total = values.reduce((sum, value) => sum + value, 0);

console.log("count=" + count);
console.log("values=" + values.join(","));
console.log("total=" + total);
generator-basics A generator function pauses at each `yield`. The caller resumes it when it asks for the next value.