I have a task that runs every second like so:
let queue = ["Sample Data 1", "Sample Data 2"]
const job = schedule.scheduleJob('*/1 * * * * *', function () {
console.log("Checking the queue...")
if (queue.length > 0) {
wss.broadcast(JSON.stringify({
data: queue[0]
}));
setTimeout(() => {
queue.shift();
}, queue[0].duration);
}
});
I am wondering how I can make it so that the timeout must finish before the next queue check. Could I use a de-bounce, or is there a better way?
You could use
to make an interval between the jobs at least 1 second and call the schedule recursively.
In that case returning a job could make not much sense, so you could collect your jobs in an array for example if needed.
But I would use an async generator to simplify the logic. A bonus here that you can execute an async code with
awaitin thefor awaitloop and its execution time will be counted also: