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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A do/while loop is ideal for pagination since you always need at least one request before knowing if more pages exist.
- 2Cursor pagination chains requests by feeding each response's next cursor into the following call until it runs dry.
- 3Building a URL with URLSearchParams keeps query parameters safely encoded and conditional ones easy to add.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
python
import csv import io from datetime import datetime
Streaming a CSV export in Flask
streaming
generators
csv
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/cursor-based-pagination-with-fetch-explained-javascript-5f55/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.