Skip to content

Instantly share code, notes, and snippets.

@chulman444
Created July 27, 2019 13:39
Show Gist options
  • Save chulman444/77f75d7f6fd008ecdb24e6f67d8697fc to your computer and use it in GitHub Desktop.
Save chulman444/77f75d7f6fd008ecdb24e6f67d8697fc to your computer and use it in GitHub Desktop.
Pagination using Axios. The property names you see here are YouTube api prop names such as `nextPageToken` and `items`.
function main() {
const axios_config:any = {
params: {
// some_params that will later accept pagination related params
},
url: "some url"
}
let all_entries:any[] = []
let response_data:any
while(true) {
// (Previous) response_data
if(response_data) {
axios_config.params.pageToken = response_data.nextPageToken
}
const response = await axios(axios_config)
response_data = response.data
all_entries = all_entries.concat(response_data.items)
// Exit condition (no more pagination thing to repeat)
if(!("nextPageToken" in response_data)) {
break
}
}
return all_entries
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment