Created
March 3, 2018 09:26
-
-
Save lemming/b8387549524a80f01be4d7d150d37ce2 to your computer and use it in GitHub Desktop.
async flow with generators example
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
function* gen() { | |
const previous = yield new Promise(resolve => setTimeout(() => resolve(1), 1000)); | |
yield new Promise(resolve => setTimeout(() => resolve(previous + 1), 1000)); | |
} | |
async function flow(gen) { | |
const iterator = gen(); | |
let result; | |
while (true) { | |
const d = iterator.next(result); | |
if (d.done) break; | |
result = await d.value; | |
} | |
return result; | |
} | |
flow(gen).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment