Async Generator Batched Iteration

Pull remote pages lazily through an async generator, batching records per yield so consumers control pacing and memory stays flat.

JavascriptData Fetching

Code

snippet
async function* paginate(source, pageSize = 100) {
  let offset = 0;
  while (true) {
    const rows = await source(offset, pageSize);
    if (rows.length === 0) return;
    yield rows;
    offset += rows.length;
  }
}

async function* flatten(batches) {
  for await (const batch of batches) {
    for (const row of batch) {
      yield row;
    }
  }
}

const remote = async (offset, limit) => {
  const res = await fetch(
    'https://api.example.com/records?offset=' + offset + '&limit=' + limit,
  );
  const body = await res.json();
  return body.rows;
};

let total = 0;
for await (const row of flatten(paginate(remote))) {
  total += row.amount;
  if (total > 10_000) break; // 消费者随时可提前退出
}
console.log('accumulated amount:', total);