Created
August 17, 2021 12:03
-
-
Save rostegg/33702d6e3e804f6a7084fcbd8602f70a to your computer and use it in GitHub Desktop.
Custom async iterator
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 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