Consuming a paginated API meant a while loop with a mutable cursor and a manual termination condition, all of it in the caller.
async function* pages(url) {
let next = url;
while (next) {
const res = await fetch(next);
const body = await res.json();
yield body.data;
next = body.links.next;
}
}
for await (const batch of pages('/api/orders')) {
await store(batch);
}
The pagination logic ends up in one place and the consumer reads like a loop over a collection, which is the whole benefit. It is sequential by construction — each iteration awaits before the next starts — so it is the wrong tool when the requests are independent and you want them concurrent. Node 10 supports it natively; earlier versions need a flag, which matters for anything still on 8 LTS.