Async While Loop Query Help

  • Goal: I'm attempting to write a JS query that loops through paginated api calls until there are no new pages in the list

  • Steps: I've tried using promise chains, then chains, on success chains. All of the asyncronicty is leading to inconsistent results. Sometimes it only returns the first page, sometimes the second, never all the pages.

  • Code:

await getFonts.trigger()
let data = getFonts.data

let availableFonts = new Map()
while(data.next){

  let tick = true
  
  getMap(availableFonts, data.items)
  //Find the next chunk of data to request from the api, regex to pull the values I want
  let load = data.next.replace(RegExp("^[^_]*="), '')
  
  await setFontLimit.trigger({additionalScope:{limit: load},
          onSuccess: getFontsByLimit.trigger({
                onSuccess: x => { data = x; console.log(x); tick = false}})})
  //wait until success
  while(tick){
    console.log("tickticktick")
    await new Promise(resolve => setTimeout(resolve, 1000))
  }
}

console.log(JSON.stringify(availableFonts))
return availableFonts

Hello @eTeall! Welcome to the forums!

I have a while loop that calls upon an API in a similar fashion -- I ended up just pushing results to a return array in this way:

const pacakgeArray = [];
let pageVar = 0;
let response = await stratusPackages(pageVar)
if(response.data.pageCount > 0)
{
  response.data.data.forEach((package, i) => {
    pacakgeArray.push({
      ...results...
    })
  })
  while(response.data.links.next){
    pageVar += 1;
    response = await stratusPackages(pageVar)
    response.data.data.forEach((package, i) => {
      pacakgeArray.push({
        ...results...
      })
    })
  }
}
return _.sortBy(pacakgeArray.filter(result => result.bcxNumber),'packageIndex')

I'm not sure how your API is dealing with the pagination, but in my case I was able to just loop page by page this way by passing the next page number in the loop.

2 Likes

Oh that worked almost out of the box for my needs. I was making it more complicated than it needed to be, thank you!

2 Likes