Back to results for “Async”
JavaScript › Async Iterators & Streams

How do you loop over an async iterable?

Beginner

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.

Common follow-ups: Can you use for-await-of on a regular (synchronous) array?