Skip to content

paginate

paginate<T>(getPage, options?): OutputType<T>

Defined in: packages/synapse-core/src/pagination.ts:108

Iterate over every item returned by a cursor-paginated action.

The generator passes each returned nextCursor back to getPage. Cursors are opaque: callers must not calculate the next value themselves. A repeated or non-advancing cursor is rejected to prevent an infinite loop.

Type Parameter
T
ParameterTypeDescription
getPageGetPage<T>Function that fetches one page for the supplied cursor.
optionsOptionsTypeInitial pagination options.

OutputType<T>

An async generator that yields items from every page.

Stream items as pages are fetched

import { paginate } from '@filoz/synapse-core'
import { getClientDataSetIds } from '@filoz/synapse-core/warm-storage'
for await (const dataSetId of paginate(({ cursor }) =>
getClientDataSetIds(client, { address, cursor })
)) {
console.log(dataSetId)
}

Accumulate every item into an array

import { paginate } from '@filoz/synapse-core'
import { getClientDataSetIds } from '@filoz/synapse-core/warm-storage'
const dataSetIds = await Array.fromAsync(
paginate(({ cursor }) => getClientDataSetIds(client, { address, cursor }))
)