Async iterators
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(resolve => setTimeout(resolve, ms)) | |
const inc = async i => (await delay(500), ++i) | |
const foo = async () => { | |
for(let i = 1; i <= 5; i = await inc(i)) | |
console.log(i) // prints 1, 2, 3, 4, 5 with a delay | |
} | |
foo() |
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(resolve => setTimeout(resolve, ms)) | |
const dec = async i => (await delay(500), --i) | |
const foo = async () => { | |
let i = 5; | |
while(i = await dec(i)) | |
console.log(i) // prints 4, 3, 2, 1 with a delay | |
} | |
foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment