Great article AJ, you can also use for-await-of but it works for async iterables, you can also use them with an array of promises.
for-await-of
Here's how you would create an async iterable.
const fetchNextPage = page => Promise.resolve(page); async function* asyncGen() { let page = 0; while (page < 10) yield fetchNextPage(page++); } for await (const g of asyncGen()) { console.log(g); }
The best part about this approach is concurrency, the async generator asyncGen only creates a new promise when asked for, and the for-await-of automatically awaits at the start of for loop, resolves it and puts it as the const g.
asyncGen
g
Thanks for your comment Kushan!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great article AJ, you can also use
for-await-of
but it works for async iterables, you can also use them with an array of promises.Here's how you would create an async iterable.
The best part about this approach is concurrency, the async generator
asyncGen
only creates a new promise when asked for, and thefor-await-of
automatically awaits at the start of for loop, resolves it and puts it as the constg
.Thanks for your comment Kushan!