Last active
February 25, 2020 20:07
-
-
Save munkacsitomi/9e9b1c2d2759912238f95d40483b9da0 to your computer and use it in GitHub Desktop.
Generators
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
function* IdGenerator() { | |
let id = 0; | |
while (true) { | |
yield ++id; | |
} | |
} | |
const idIterator = IdGenerator(); | |
const ninja1 = { id: idIterator.next().value }; | |
const ninja2 = { id: idIterator.next().value }; | |
const ninja3 = { id: idIterator.next().value }; | |
console.log(ninja1.id === 1, 'First ninja has id 1'); | |
console.log(ninja2.id === 2, 'Second ninja has id 2'); | |
console.log(ninja3.id === 3, 'Third ninja has id 3'); |
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
function* WeaponGenerator() { | |
yield 'Katana'; | |
yield 'Wakizashi'; | |
yield 'Kusarigama'; | |
} | |
for (let weapon of WeaponGenerator()) { | |
console.log(weapon !== undefined, weapon); | |
} |
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
function* WeaponGenerator() { | |
yield 'Katana'; | |
yield 'Wakizashi'; | |
} | |
const weaponsIterator = WeaponGenerator(); | |
const result1 = weaponsIterator.next(); | |
console.log( | |
typeof result1 === 'object' && result1.value === 'Katana' && !result1.done, | |
'Katana received!' | |
); | |
const result2 = weaponsIterator.next(); | |
console.log( | |
typeof result2 === 'object' && result2.value === 'Wakizashi' && !result2.done, | |
'Wakizashi received!' | |
); | |
const result3 = weaponsIterator.next(); | |
console.log( | |
typeof result3 === 'object' && result3.value === undefined && result3.done, | |
'There are no more results!' | |
); |
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
function* WeaponGenerator() { | |
yield 'Katana'; | |
yield 'Wakizashi'; | |
} | |
const weaponsIterator = WeaponGenerator(); | |
var item; | |
while (!(item = weaponsIterator.next()).done) { | |
console.log(item !== null, item.value); | |
} |
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
function* WarriorGenerator() { | |
yield 'Sun Tzu'; | |
yield* NinjaGenerator(); | |
yield 'Genghis Khan'; | |
} | |
function* NinjaGenerator() { | |
yield 'Hatori'; | |
yield 'Yoshi'; | |
} | |
for (let warrior of WarriorGenerator()) { | |
console.log(warrior !== null, warrior); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment