javascript 24 lines · 6 steps

Cursor-based pagination with fetch

An async loop walks every page of a cursor-paginated API and collects all rows into one array.

Explained by highlit
1async function fetchAllPages(baseUrl, { pageSize = 100, headers = {} } = {}) {
2 const results = [];
3 let cursor = null;
4 
5 do {
6 const url = new URL(baseUrl);
7 url.searchParams.set('limit', String(pageSize));
8 if (cursor) url.searchParams.set('cursor', cursor);
9 
10 const res = await fetch(url, {
11 headers: { Accept: 'application/json', ...headers },
12 });
13 
14 if (!res.ok) {
15 throw new Error(`Request failed: ${res.status} ${res.statusText}`);
16 }
17 
18 const { data, next_cursor: nextCursor } = await res.json();
19 results.push(...data);
20 cursor = nextCursor ?? null;
21 } while (cursor);
22 
23 return results;
24}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A do/while loop is ideal for pagination since you always need at least one request before knowing if more pages exist.
  2. 2Cursor pagination chains requests by feeding each response's next cursor into the following call until it runs dry.
  3. 3Building a URL with URLSearchParams keeps query parameters safely encoded and conditional ones easy to add.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Cursor-based pagination with fetch — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code