Async Generator Use Case: Paginated APIs
You'll learn how async generators provide an elegant solution for working with paginated API endpoints by yielding results as each page is fetched.
The Pagination Problem
Many APIs return large datasets in chunks (pages). Traditional approaches require manually tracking page numbers, making multiple fetch calls, and assembling results. This gets messy quickly.
Async Generators to the Rescue
Async generators naturally model pagination: each yield represents one item from the current page, and each await fetches the next page when needed. The caller doesn't need to know about pages at all—they just iterate.
Think of it like this: Instead of downloading an entire book and then reading it, you get one page delivered at a time, automatically requesting the next page only when you're ready.
The Pattern
async function* fetchAllUsers(apiUrl) {
let nextPage = `${apiUrl}?page=1`;
while (nextPage) {
const response = await fetch(nextPage);
const data = await response.json();
// Yield each user individually
for (const user of data.users) {
yield user;
}
// API tells us the next page URL (or null if done)
nextPage = data.nextPage;
}
}
// Consume it cleanly
for await (const user of fetchAllUsers('/api/users')) {
console.log(user.name); // Process one user at a time
// Can break early without fetching remaining pages
}
The generator handles all pagination logic internally. Each user is yielded as soon as it's available, keeping memory usage low. If you break early, unnecessary pages are never fetched.
Key Takeaway: Async generators transform complex paginated API workflows into simple, memory-efficient streams where items are yielded incrementally as pages are fetched behind the scenes.