Created
July 27, 2019 13:39
-
-
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`.
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
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