paginate.js 795 B

123456789101112131415161718192021222324
  1. import { iterator } from "./iterator";
  2. export function paginate(octokit, route, parameters, mapFn) {
  3. if (typeof parameters === "function") {
  4. mapFn = parameters;
  5. parameters = undefined;
  6. }
  7. return gather(octokit, [], iterator(octokit, route, parameters)[Symbol.asyncIterator](), mapFn);
  8. }
  9. function gather(octokit, results, iterator, mapFn) {
  10. return iterator.next().then((result) => {
  11. if (result.done) {
  12. return results;
  13. }
  14. let earlyExit = false;
  15. function done() {
  16. earlyExit = true;
  17. }
  18. results = results.concat(mapFn ? mapFn(result.value, done) : result.value.data);
  19. if (earlyExit) {
  20. return results;
  21. }
  22. return gather(octokit, results, iterator, mapFn);
  23. });
  24. }