Skip to content

Instantly share code, notes, and snippets.

@lucasjellema
Created April 24, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasjellema/3b11e5f0b5e69e51f4fe2145128eb388 to your computer and use it in GitHub Desktop.
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
// 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