@clickup/rest-client
Version:
A syntax sugar tool around Node fetch() API, tailored to work with TypeScript and response validators
32 lines • 1.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Keeps calling a function with an updating cursor, and depaginates all the
* results until the cursor returned is null or undefined.
*
* On each call, the inner function needs to return an array with two elements:
* 1. Array or results, which could be empty, but not null or undefined.
* 2. A new cursor.
*/
async function* depaginate(readFunc) {
let prevCursor = undefined;
let cursor = undefined;
for (;;) {
let items;
[items, cursor] = await readFunc(cursor === null ? undefined : cursor);
yield* items;
if (cursor === null || cursor === undefined) {
break;
}
if (JSON.stringify(prevCursor) === JSON.stringify(cursor)) {
throw Error("Depagination got stuck: prevCursor=" +
JSON.stringify(prevCursor) +
", cursor=" +
JSON.stringify(cursor) +
" (they must differ)");
}
prevCursor = cursor;
}
}
exports.default = depaginate;
//# sourceMappingURL=depaginate.js.map
;