Skip to content

Read multiple pages

Use the packages from Make a GitHub request. To get the first ten open pull requests, stop iterating as soon as you have ten. GitHub can return a final page with fewer results.

import { Effect } from "effect"
import { Octokit } from "octokit"
const titles = Effect.tryPromise({
try: async signal => {
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN, request: { signal } })
const pages = octokit.paginate.iterator("GET /repos/{owner}/{repo}/pulls", {
owner: "octokit", repo: "octokit.js", state: "open", per_page: 5,
})
const result: string[] = []
for await (const { data } of pages) {
for (const pr of data) {
result.push(pr.title)
if (result.length === 10) return result
}
}
return result
},
catch: cause => new Error("Could not list pull requests", { cause }),
})
console.log(await Effect.runPromise(titles))

Create the iterator inside the operation so a second run starts on page one. In the pinned SDK, passing the signal only to the route iterator missed both fetches in a two-page experiment. The example sets the signal on a client created for this run. The probes also cover putting it in the method’s defaults. The iterator follows GitHub’s next-page link even when a page contains zero records.

See the experiment · Look up paginated routes