Last active
January 19, 2019 02:16
-
-
Save nybblr/6600a7d2fbd9e36fc09ae9ddd10a0b02 to your computer and use it in GitHub Desktop.
Tiny example of async generator functions
This file contains hidden or 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
let timer = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
let producer = async function*() { | |
let counter = 0; | |
while (true) { | |
let delay = Math.random() * 1000; | |
await timer(delay); | |
yield counter++; | |
} | |
}; | |
let consumer = async (source) => { | |
for await (let value of source) { | |
console.log(value); | |
} | |
}; | |
consumer(producer()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment