Dear community,
I have spent a full day on this, I'm just about to
Any help would be greatly appreciated !
-
Goal:
- I am trying to process a list of locations in batches using JavaScript within Retool. My goal is to make batched API calls to an external service (e.g., Google Maps API) for each group of locations in order to reduce the number of goals and the related API fees. I expect each batch to process a subset of the total locations and then aggregate the results. Do you know if this makes any sense or if making one call per row would result in the same cost structure ?
-
Steps:
- I have created a JavaScript function that divides the locations from the array into batches and then triggers an asynchronous API call for each batch. I've used
Promise.all
to handle the asynchronous nature of these calls. However, I am encountering an issue where only the last item in the locations array is processed and returned, instead of the entire batch.
- I have created a JavaScript function that divides the locations from the array into batches and then triggers an asynchronous API call for each batch. I've used
-
Details:
- Here's the relevant part of my JavaScript code in Retool:
async function processInBatches(batchSize) {
const locations = getLocations(); //returns an array ["address1","...","address220"]
const results = [];
let batch = [];
for (const [index, value] of locations.entries()) {
batch.push(value);
if (batch.length === batchSize || index === locations.length - 1) {
const promise = query10_lambda.trigger({ additionalScope: { locations: batch } });
results.push(promise);
batch = [];
}
}
const allResults = await Promise.all(results);
return allResults;
}
-
- Each time I run this script, it only outputs the result for the last location of the array instead of all the batched locations. I suspect there might be an issue with how the batches are being processed or how the asynchronous calls are being handled.
-
Screenshots:
Any idea ?