admataz
10/6/2017 - 2:31 PM

access a complete paginated recordset, iterating one page at a time using a generator function

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!
      })
  }
}