Skip to content

Instantly share code, notes, and snippets.

@andras-hegedus
Last active July 30, 2020 15:09
Show Gist options
  • Save andras-hegedus/6b9ecb8aa0d0614d38ed4bcf40094798 to your computer and use it in GitHub Desktop.
Save andras-hegedus/6b9ecb8aa0d0614d38ed4bcf40094798 to your computer and use it in GitHub Desktop.
TypeScript: increment numbers with generator function
// based on MDN's generator example
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
function* incNumbers(): Generator<number, never, number> {
let index = 0;
while (true) {
yield index++;
}
}
const gen = incNumbers()
gen.next().value // 0
gen.next().value // 1
gen.next().value // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment