I am making a SaaS application using Square. For performance reasons when I have to fetch different square api’s within the same functions. I have used Promise.all to ensure all the required request are fetched simoultaneously.But the wierd thing I noticed is that even is I initiate the requests simoultaneously using Promise.all, Square SDK internally makes the request sequentially.
So I made a testing snippet which makes 4 different api call using the sdk and sends them paralelly.
const catalogSearch = squareClient.catalog.search({
includeRelatedObjects: true,
}).then(result => {
const endTime = Date.now();
console.log(`Catalog search completed at ${endTime - test4StartTime}ms`);
return result;
}).catch(error => {
const endTime = Date.now();
console.error(`Catalog search failed at ${endTime - test4StartTime}ms:`, error);
throw error;
});
const catalogSearchItems = squareClient.catalog.searchItems({}).then(result => {
const endTime = Date.now();
console.log(`Catalog searchItems completed at ${endTime - test4StartTime}ms`);
return result;
}).catch(error => {
const endTime = Date.now();
console.error(`Catalog searchItems failed at ${endTime - test4StartTime}ms:`, error);
throw error;
});
const locationsList = squareClient.locations.list().then(result => {
const endTime = Date.now();
console.log(`Locations list completed at ${endTime - test4StartTime}ms`);
return result;
}).catch(error => {
const endTime = Date.now();
console.error(`Locations list failed at ${endTime - test4StartTime}ms:`, error);
throw error;
});
const locationGet = squareClient.locations.get({ locationId: 'L097HCJ5BRQPS' }).then(result => {
const endTime = Date.now();
console.log(`Location get completed at ${endTime - test4StartTime}ms`);
return result;
}).catch(error => {
const endTime = Date.now();
console.error(`Location get failed at ${endTime - test4StartTime}ms:`, error);
throw error;
});
console.log('All 4 different endpoint requests started simultaneously');
await Promise.all([catalogSearch, catalogSearchItems, locationsList, locationGet]);
const test4TotalTime = Date.now() - test4StartTime;
console.log(`All 4 different endpoint requests completed in ${test4TotalTime}ms`);
The console logs achieved from running the following snippet are:Testing calling
different endpoints in parallel:All 4 different endpoint requests started simultaneouslyLocations list completed at 1008msCatalog searchItems completed at 3394msLocation get completed at 3398msCatalog search completed at 6155msAll 4 different endpoint requests completed in 6155m
I then followed to manually run paralell request using manual fetch requests for larger number of simoultaneous request. Just as expected through manual fetch request I can ahieve high concurrency. Whenn using the sdk the requests always go through sequentially.
I tried to search if this is a known issue, or if it is intentional but I was not able to find anything relevant.
How can I send multiple requests concurently using the Square SDK?