Skip to content

Instantly share code, notes, and snippets.

@rostegg
Created August 17, 2021 12:03
Show Gist options
  • Save rostegg/33702d6e3e804f6a7084fcbd8602f70a to your computer and use it in GitHub Desktop.
Save rostegg/33702d6e3e804f6a7084fcbd8602f70a to your computer and use it in GitHub Desktop.
Custom async iterator
let range = {
from: 1,
to: 5,
[Symbol.asyncIterator]() {
return {
current: this.from,
last: this.to,
async next() {
await new Promise(resolve => setTimeout(resolve, 1000))
if (this.current <= this.last) {
return { done: false, value: this.current++ }
} else {
return { done: true }
}
}
}
}
}
(async () => {
for await (let value of range) {
console.log(value)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment