I have a database with a lot of items and I need to perform an action on each of these items.Unfortunatly, I have to run these actions sequentially and also delay every action to avoid rate limit.
My approach does not wait for the previous actions to be finished and I end up getting rate limited. What do I have to change in order for it to run sequentially?
setInterval(async () => {
await this.processQueue();
}, 1500)
private async processQueue() {
try {
//Only 3 requests per second allowed by the API so I only take 3 items from the database on every call
const bids = await getRepository(Bid).find({ order: { created_at: "ASC" }, take: 3, skip: 0 })
if (bids) {
for (const bid of bids) {
//check if active order exists
const activeOrder = await this.accountService.hasActiveOrder(bid.tokenId, bid.tokenAddress, 0);
if (!activeOrder) {
//perform async functions with that db item
const startTime = Date.now();
await this.placeBid(bid);
//delete from database so the next call to processQueue does not return the same itemsagain
await getRepository(Bid).delete({ id: bid.id })
const endTime = Date.now() - startTime;
}
}
}
} catch (error) {
console.error("TradingService processQeueu", error.message);
}
}