Skip to content

Instantly share code, notes, and snippets.

@marcoalbarelli
Last active March 14, 2019 16:59
Show Gist options
  • Save marcoalbarelli/c6f07619765e1802e6d5a806693b21b4 to your computer and use it in GitHub Desktop.
Save marcoalbarelli/c6f07619765e1802e6d5a806693b21b4 to your computer and use it in GitHub Desktop.
async+generators
const g = function* (array) {
for(let e of array) {
yield new Promise((resolve, reject) => {
return setTimeout(()=>{resolve(e)}, 1000)
})
}
}
for(let f of g([1,100,23])) {
f.then(console.log)
}
const g2 = g([64,298,112])
const waiting = () => {
console.log('waiting')
const yieldedPromise = g2.next().value
if(yieldedPromise) {
yieldedPromise
.then(console.log)
.then(waiting)
}
}
waiting()
const waiting2 = async() => {
for(let f of g([1234,198700,8823])) {
await f.then(console.log)
console.log('waiting2')
}
}
waiting2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment