【发布时间】:2022-01-19 17:38:23
【问题描述】:
我有一个包含很多项目的数据库,我需要对这些项目中的每一个执行操作。不幸的是,我必须按顺序运行这些操作并延迟每个操作以避免速率限制。
我的方法不会等待之前的操作完成,我最终会受到速率限制。我必须更改什么才能使其按顺序运行?
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);
}
}
【问题讨论】:
标签: node.js typescript typeorm