Created
February 7, 2016 06:36
-
-
Save jung-kim/10d8f3680f82e26495d5 to your computer and use it in GitHub Desktop.
Obvious benefits of generator
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
// Generator, doing async without promise looking like sync... | |
function* countWithGenerator(n){ | |
for (var x = 0; x < n; x++) { | |
yield x | |
console.log('fetched!') | |
} | |
} | |
function countInteratively(n){ | |
var res = [] | |
for (var x = 0; x < n; x++) { | |
res.push(x) | |
console.log('fetched!') | |
} | |
return res | |
} | |
console.log('==== counting with generator ====') | |
for (var x of countWithGenerator(5)) { | |
console.log(x) | |
} | |
console.log('\n\n\n') | |
console.log('==== counting with iterator ====') | |
for (var x of countInteratively(5)) { | |
console.log(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment