Last active
July 30, 2020 15:09
-
-
Save andras-hegedus/6b9ecb8aa0d0614d38ed4bcf40094798 to your computer and use it in GitHub Desktop.
TypeScript: increment numbers with generator function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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