question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to paginate with token? (or recursively call async function and pass through parameters)

See original GitHub issue

AWS API calls typically respond with a pagination token. When present, another call to the same API should be made with that token, until a token is no longer supplied. This is typically done using recursion.

I would like to use async (or another library) to simplify this approach. I had imagined the following psuedo code:

  await paginate(async (lastToken, recurse, page) => {
    const { certificates, token } = await listCertificates({nextToken: lastToken})
    handleCertificates(certificates)
    token && recurse(token)
  })

This illustrates how something as complicated as token based pagination could be accomplished with a few lines of code. I’ve looked into async and saw some relevant functions (e.g. until), but have not found a way to recursively pass the output of one call to the next (i.e. pass in the token to the next call).

Is this possible with async? Or is there a library that’s more suitable for this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
aearlycommented, Dec 10, 2017

doWhilst is good for this kind of thing.

let results = []
let nextToken
doWhilst(
  (next) => {
    getPage(url, nextToken, (err, body) => {
      if (err) return next(err);
      nextToken= body.nextToken
      results = results.concat(body.objects)
      next()
    })
  },
  () => nextToken,
  (err) => {
    // results now has all pages
  })

Although, with async/await, it’s easier to use a plain do/while:

let results = []
let nextToken
do {
  const body = await getPage(url, opts, nextToken)
  nextToken = body.nextToken
  results = results.concat(body.objects)
} while (nextToken)
// results now has everything
0reactions
tommedemacommented, Dec 10, 2017

@aearly I like how async/await makes something like do/while so simple to read again; thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Practical Asynchronous Recursion in JavaScript - Medium
The function for listing a bucket 'listObjectsV2' is a paginated listing, so after each call the caller can check to see if another...
Read more >
Async/Await for recursive API Calls - javascript - Stack Overflow
All async functions return a promise, always. So both getSelectAllIds() and someFunction() will always return a promise.
Read more >
Asynchronous Recursion with Callbacks, Promises and Async.
The first approach we'll look at is a little old-fashioned, using callback functions. The first step is to change the getSentenceFragment ...
Read more >
Pagination or while loop using recursion in Mule 4 - Mulesy.com
This Tutorial talks about how to make use of recursion in Mule 4 to solve pagination and while loop scenarios.
Read more >
octokit/rest.js
All endpoint methods are asynchronous, in order to use await in the code ... To stop paginating early, you can call the done()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found