Take a limited number of items from a potentially infinite generator
I would write it as:

function* take(n, generator) {
  let values = generator();

  let i = 0;
  while (i++ &lt; n) {
    yield values.next().value;
  }
}


this seems to match the intent more closely though its mostly a matter of personal preference.

Update

You can also add the take method as an extension to the generator so you could call it like generator().take(3). See this SO answer if you are interested in that approach.