Parsing More Than One Page From Api
I am trying to fetching eventbrite's API formatted as JSON, the issue is JSON can only have 50 Items, which means the Json does not have all the information. so here https://www.
Solution 1:
You should probably try to match the pagination and not get all the pages at once, but if you really need to do it, I don't think it's a good idea to setState
so many times -- ideally you would make all the requests and setState
once at the end. Something like this:
Batch up all the requests into an array of promises:
let requests = [];
for (let i=0; i<32; i++) {
requests.push(fetch('https://www.eventbriteapi.com/v3/events/search/?location.address=glasgow&page='+i+'&token=XXX&expand=venue').then(r => r.json()));
}
Run all the requests at once and wait for them all to finish, then do the setState once all the requests have finished:
Promise.all(requests).then((responses) => {
let arr = [];
for (let response of responses) {
for (let event of response.events) {
// extract the event info and add it to the individual arrays/items// ...
Also, not sure how your app is structured to use this data, but it might be easier to just keep it in one object instead of having separate arrays for each:
{
name,
latitude,
longitude,
url
}
Post a Comment for "Parsing More Than One Page From Api"