Created
October 6, 2017 14:31
-
-
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
This file contains 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
/** | |
* 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