Skip to content

Instantly share code, notes, and snippets.

@lemming
Created March 3, 2018 09:26
Show Gist options
  • Save lemming/b8387549524a80f01be4d7d150d37ce2 to your computer and use it in GitHub Desktop.
Save lemming/b8387549524a80f01be4d7d150d37ce2 to your computer and use it in GitHub Desktop.
async flow with generators example
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