Created
April 27, 2016 15:24
-
-
Save dignifiedquire/e2f6b5f948d4abe36b676e5d45272791 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const _ = require('lodash') | |
const Promise = require('bluebird') | |
module.exports = function loadAllPages (callFx, opts) { | |
opts['page'] = opts.page || 1 | |
const res = [] | |
return Promise | |
.resolve(callFx(opts)) | |
.then((result) => { | |
if (result && result.length > 0) { | |
res.concat(result) | |
if (_.isFunction(result.nextPage)) { | |
opts.page = opts.page + 1 | |
return loadAllPages(callFx, opts) | |
} | |
} | |
return res | |
}) | |
.catch((err) => { | |
if (err) { | |
console.log('Failed to depaginate!\n', err) | |
console.log('Options:', opts) | |
console.trace() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment