Here is a snippet of my code:
async run(minutesToRun: number): Promise<void> {
await authenticate();
await this.stock.fillArray();
await subscribeToInstrument(this, this.orderBookId);
await subscribeToOrderbook(this, this.orderBookId);
await this.updateLoop(minutesToRun);
}
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async updateLoop(minutesToRun: number) {
const startTime = new Date();
const timeDiff = compareTimestamps(startTime, this.currentTime);
while (timeDiff < minutesToRun) {
console.log(timeDiff);
this.currentTime = new Date();
this.timeStampArray.push(this.currentTime);
console.log(this.timeStampArray);
await this.stock.updateData();
await this.sleep(60000);
}
}
Although I prioritize having the updateData call occur every minute for data analysis purposes rather than ensuring it runs before other functions, I am experiencing fluctuation in the timing. The interval ranges from 1m 100ms to 1m 300ms. How can I guarantee consistency in the minute intervals even if other functions are executed in between?