I am running 3 API requests and they are making my JS script very slow.
The goal is to fetch data from the API and push it my database.
First 2 API is called using date and next_page_token. So until there is a next page token i keep calling these 2 APIs. I call them recursively.
I store the id that i get from above in an array and pass it to the next 2 APIs.
The last API call run in a loop. I loop through the ids and call the API each time.
Code:
export async function getFirstAPI(access_token, start, end, globalObject){
let url = 'baseAPI/meetings/from=start/to=end/next_page_token=globalObject.next_page_token';
var obj = {
method: 'GET',
headers: {
authorization: 'Bearer {yourtokenhere}'
}
}
let response = await fetch(url, obj)
let data = await response.json()
return data
}
export async function getSecondAPI(access_token, start, end, globalObject){
let url = 'baseAPI/chats/from=start/to=end/next_page_token=globalObject.next_page_token';
var obj = {
method: 'GET',
headers: {
authorization: 'Bearer {yourtokenhere}'
}
}
let response = await fetch(url, obj)
let data = await response.json()
return data
}
export async function getThirdAPI(access_token, id_array, globalObject){
for(let i=0; i<id_array.length; i ){
let url = 'baseAPI/participants/{id}';
var obj = {
method: 'GET',
headers: {
authorization: 'Bearer {yourtokenhere}'
}
}
let response = await fetch(url, obj)
let data = await response.json()
globalObject.store_data.push(data)
}
return globalObject
}
When i run the above for a single day. That alone takes 14min 20sec. If i run it for a wider date range i am guessing this will go on for hours!!
Is there a way i can optimize this code? Or is it supposed to take this much time to run?
Any suggestions would be great!!
CodePudding user response:
Switch from await
in your loop to Promise.all
or Promise.allSettled
. It will work much faster. Await is not a silver bullet. It helps but in your case you are waiting for a response on each iteration instead of.. something like "fire all and collect results"
export async function getThirdAPI(access_token, id_array, globalObject) {
const apiPromises = id_array.map(async (id) => {
let url = `${baseAPI}/participants/${id}`;
var obj = {
method: 'GET',
headers: { /* */ },
authorization: `Bearer ${access_token}`
}
const response = await fetch(url, obj);
return await response.json();
});
const results = await Promise.all(apiPromises);
// in case globalObject.store_data is an array
globalObject.store_data.push(...results);
return globalObject;
}