Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES 2018 Asynchronous Iterator (async generator function and await for..of loop) - foundation for pipelined and nevery ending generator functions
// 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
You can’t perform that action at this time.