I have Promise.allSettled
code and inside the iterable, I call an API. It will increase API calls significantly if I have much data. So I want to reduce the API calls. I think I can achieve it if I call the API every 5/10 seconds. How to set the delay from each iterable item inside the .map
?
//API
async function searchMerchant(merchantName) {
try {
const indexs = await index.search(merchantName, {
attributesToRetrieve: ["merchant_id", "name"],
restrictSearchableAttributes: ["name"],
typoTolerance: false,
hitsPerPage: 1,
});
return indexs.hits;
} catch (error) {
console.error(error);
return error
}
}
Promise.allSettled(
merchants.map(async (merchant) => {
//how to set delay inside this block
const merchantDetails = await searchMerchant(merchant.name);
return merchantDetails;
}
).then(res => console.log(res))