Created
April 24, 2019 19:23
-
-
Save lucasjellema/3b11e5f0b5e69e51f4fe2145128eb388 to your computer and use it in GitHub Desktop.
ES 2018 Asynchronous Iterator (async generator function and await for..of loop) - foundation for pipelined and nevery ending generator functions
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
// asynchronous generator - read in await for..of loop | |
const sleep = (milliseconds) => { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
} | |
const lg = (msg) => { | |
const d = new Date() | |
console.log(`${d.getSeconds()}:${d.getMilliseconds()} - ${msg}`) | |
} | |
const alphabet = async function* () { | |
var n = 0 | |
while (n < 26) { | |
await sleep(500) | |
lg(`.. yield ${n}`) | |
yield String.fromCharCode(97 + n++); | |
} | |
} | |
const doSomething = async () => { | |
//print the alphabet | |
for await (let ch of alphabet()) { | |
lg(ch) | |
}// for of alphabet() | |
} | |
doSomething() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment