nas5w / javascript-tips-and-tidbits

A continuously-evolving compendium of javascript tips based on common areas of confusion or misunderstanding.
MIT License
1.2k stars 72 forks source link

Example using generators with for..of and spread #27

Open tomscallon opened 3 years ago

tomscallon commented 3 years ago

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]