Hint
function* + async = yield Promises lazily; consumed with for await...of
Async generators combine generator syntax with async/await β they yield values asynchronously and are consumed with for await...of.
// Async generator β paginated API fetcher
async function* fetchPages(url) {
let page = 1;
while (true) {
const res = await fetch(`${url}?page=${page++}`);
const { items, hasMore } = await res.json();
yield items; // pause, give back items, resume on next iteration
if (!hasMore) return; // done
}
}
// Consume with for await...of
async function loadAll() {
const allItems = [];
for await (const items of fetchPages('/api/data')) {
allItems.push(...items);
if (allItems.length >= 100) break; // can stop early!
}
return allItems;
}
// Streaming data (Fetch Streams API)
async function* streamLines(url) {
const reader = (await fetch(url)).body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) { if (buffer) yield buffer; return; }
buffer += decoder.decode(value);
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) yield line;
}
}