Last active
March 14, 2019 16:59
-
-
Save marcoalbarelli/c6f07619765e1802e6d5a806693b21b4 to your computer and use it in GitHub Desktop.
async+generators
This file contains 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
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