Created
May 6, 2017 09:56
-
-
Save SergeyLipko/661633ba6e6a1e01a93032d74aaba670 to your computer and use it in GitHub Desktop.
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 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