Skip to content

Instantly share code, notes, and snippets.

@admataz
Created October 6, 2017 14:31
Show Gist options
  • Save admataz/bf0f29b6295985f5b05379d80c9523df to your computer and use it in GitHub Desktop.
Save admataz/bf0f29b6295985f5b05379d80c9523df to your computer and use it in GitHub Desktop.
access a complete paginated recordset, iterating one page at a time using a generator function
/**
* Generator for iterating paginated results
*/
function * Paginator () {
let next = true
let item = null
while (next) {
if (item) {
item = yield item.page + 1
} else {
item = yield 1
}
next = item.page * item.limit < item.total
}
}
/**
* Get all records from the endpoint, accounting for pagination
*
* @param {Generator} Paginator - handles loading paginated response
*
* @return {Promise<array>} - results of all pages
*/
const getPagedData = (Paginator) => {
const g = Paginator()
let records = []
return more(g.next())
// recursive function to get results and next() the generator
function more (item) {
if (item.done) {
return Promise.resolve(records)
}
return asyncFetchRequst({page: item.value, limit: 100})
.then(result => {
records = [...records, ...result.data]
return more(g.next(result)) // do it all over again!
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment