SyntaxStudy
Sign Up
JavaScript Advanced 5 min read

Async Iteration

for await...of

Async iterables yield values asynchronously. Use for await...of to consume streams, paginated APIs, and async generators.

Example
async function* paginate(url) {
  let page = 1, hasMore = true;
  while (hasMore) {
    const { data, next } = await fetch(`${url}?page=${page}`).then(r => r.json());
    yield data;
    hasMore = !!next; page++;
  }
}
for await (const batch of paginate("/api/items")) {
  processBatch(batch);
}
Pro Tip

Async generators + for await...of are the idiomatic way to consume streaming or paginated data.