Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created January 6, 2017 06:31
Show Gist options
  • Save myndzi/d3bdd89812076953ad238f6190c16a7f to your computer and use it in GitHub Desktop.
Save myndzi/d3bdd89812076953ad238f6190c16a7f to your computer and use it in GitHub Desktop.
// paginate('get', '/foo', {...})
const paginate = (method, path, opts, offset, results) => {
offset = offset || 0;
results = results || [ ];
return Promise.try(() =>
request(method, path, Object.assign(opts, { offset }))
).then(res => {
let newResults = results.concat(res.data);
if (!res.pagination) { return newResults; }
if (offset + res.pagination.limit > res.pagination.total) { return newResults; }
return paginate(method, path, opts, offset + res.pagination.limit, newResults);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment