Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created March 19, 2022 14:40
Embed
What would you like to do?
Async iterators
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()
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