Skip to content

Instantly share code, notes, and snippets.

@lffg
Last active September 11, 2021 21:41
Show Gist options
  • Select an option

  • Save lffg/36ec39a2b15c4174c1c727dbb8eb9e08 to your computer and use it in GitHub Desktop.

Select an option

Save lffg/36ec39a2b15c4174c1c727dbb8eb9e08 to your computer and use it in GitHub Desktop.
function* gen() {
yield 1;
yield 2;
return 3;
}
function* withReturned(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr;
while (
!(curr = iterator.next()).done ||
(curr.done && typeof curr.value !== 'undefined')
) {
yield curr.value;
}
}
console.log([...gen()]); //=> [1, 2]
console.log([...withReturned(gen())]); //=> [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment