Skip to content

Function: paginateByOffset()

@kortexya/reasoninglayer


@kortexya/reasoninglayer / paginateByOffset

paginateByOffset<TItem>(read, options?, requestOptions?): AsyncGenerator<TItem, void, undefined>

Defined in: src/pagination.ts:135

Iterate a limit/offset endpoint, one item at a time.

TItem

OffsetPageReader<TItem>

Reads one page.

OffsetPaginateOptions

Page size, an optional overall cap, the offset to start at, and an onPage callback that sees each page’s total.

RequestOptions

Per-call options, applied to EVERY page request.

AsyncGenerator<TItem, void, undefined>

An async iterator over the items.

A signal in requestOptions cancels the whole walk, not just the page in flight — which is the behaviour a caller breaking out of a for await wants, and the reason the options are threaded into each page rather than the first.

When the page reports total, the walk ends at total (or on an empty page), NOT on a short page: an engine that caps a page below the requested size answers a short page with rows still to come. Only a page without a total ends the walk by being short.

for await (const term of client.terms.iterateTerms({ sortName: 'employee' })) {
console.log(term.id);
}
// Resume after a page already read, and keep the total beside the rows.
const first = await client.terms.listTerms({ limit: 500 });
let total = first.total;
for await (const term of client.terms.iterateTerms(undefined, {
offset: first.terms.length,
onPage: (page) => (total = page.total),
})) {
console.log(term.id);
}