Advertisement
Beginner JavaScript, Async Iterators & Streams

How do you loop over an async iterable?

Short answer

Use a for-await-of loop, which automatically awaits each next() call and unwraps the resolved value, pausing the loop until each item is ready.

Save JavaScript mock interview
Advertisement
AModel answer
Use a for-await-of loop, which automatically awaits each next() call and unwraps the resolved value, pausing the loop until each item is ready.
async function run() {
  for await (const chunk of asyncIterable) {
    console.log(chunk);
  }
}
Real-world example
Streaming and logging each chunk of a fetch() response body as it arrives.
Likely follow-ups
  • Can you use for-await-of on a regular (synchronous) array?
Related topics