Skip to content

Instantly share code, notes, and snippets.

@SergeyLipko
Created May 6, 2017 09:56
Show Gist options
  • Save SergeyLipko/661633ba6e6a1e01a93032d74aaba670 to your computer and use it in GitHub Desktop.
Save SergeyLipko/661633ba6e6a1e01a93032d74aaba670 to your computer and use it in GitHub Desktop.
const delay = ms => new Promise(res => setTimeount(res, ms));
async function * gen() {
await delay(1000);
yield 1;
await delay(1000);
yield 2;
await delay(1000);
yield 3;
}
// асинхронная итерация
async function main() {
for await (const value of gen()) {
console.log(value);
}
}
// аналог
async function def() {
const generator = gen();
while (true) {
const { value, done } = await generator.next();
if (done) {
break;
}
console.log(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment