Skip to content

Instantly share code, notes, and snippets.

@jung-kim
Created February 7, 2016 06:36
Show Gist options
  • Save jung-kim/10d8f3680f82e26495d5 to your computer and use it in GitHub Desktop.
Save jung-kim/10d8f3680f82e26495d5 to your computer and use it in GitHub Desktop.
Obvious benefits of generator
// 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