Created
March 19, 2022 14:40
-
-
Save yairEO/9204a5f22d6a3c5abe58d4cff76ca4d7 to your computer and use it in GitHub Desktop.
Async iterators
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
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 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
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