nas5w/javascript-tips-and-tidbits

Example using generators with for..of and spread

tomscallon opened this issue · 0 comments

E.g. with

function* numbers(x) {
  yield 1;
  yield 2;
  yield 3;
  yield x;
}

For..of:

for (const x of numbers(99)) {
  console.log(x);
}
// => 1
// => 2
// => 3
// => 99

Spread:

const arr = [...numbers(99)];
console.log(are);
// => [1, 2, 3, 99]