I could not implement PQueue as per your suggestion @MikeCB because it did not seem compatible with Retool. The library is added via a CDN link, but is not accessible from queries. Maybe it's the same problem as described here. Anyways, instead I found an alternative called Bottleneck.
Here's my implementation for everyone interested in throttling POST requests:
const limiter = new Bottleneck({
maxConcurrent: 1,
minTime: 1000 // At most 1 request per second
});
const uniqueData = formatDataAsArray(getUniqueCurrencies.data)
const results = {};
// Use Promise.all to wait for all scheduled tasks to complete
await Promise.all(uniqueData.map(data => {
const key = `${data.date}_${data.source}`;
if (data.source === 'EUR') {
results[key] = 1; // Directly assign value if source is 'EUR'
return Promise.resolve(); // Immediately resolve for 'EUR'
} else {
// Schedule the task and update results within the promise
return limiter.schedule(() =>
getConversionRates.trigger({
additionalScope: {
conversionDate: data.date,
sourceCurrency: data.source,
targetCurrency: 'EUR',
},
}).then(res => {
results[key] = res.data;
}));
}
}));
return results;
It works and it's faster than before. Sometimes I would notice "rate exceeded" warnings, but I guess I could increase the minTime to make sure it observes more than a second between requests. I'm sure this snippet could be improved.
Thanks for your help!
J.