🟨 JavaScript Intermediate

What is async iteration in JavaScript?

Answer

Async iterators and async generators (ES2018) enable iterating over asynchronous data sources — APIs that deliver data over time (database cursors, streams, paginated APIs). An async iterable implements Symbol.asyncIterator returning an async iterator whose next() returns a Promise of {value, done}. Consume with for await...of: for await (const chunk of asyncIterable) { process(chunk); }. Async generator functions (async function*) combine async and generator: async function* paginate(url) { while (url) { const {data, nextUrl} = await fetch(url).then(r => r.json()); yield* data; url = nextUrl; } }. for await (const item of paginate("/api/items")) { }. This cleanly handles pagination, streaming responses, and any push-based data source. The ReadableStream browser API is also async iterable in modern browsers, enabling for await (const chunk of stream).