我想安排一个异步函数(async/await ruturn 类型)每两分钟运行一次。
我尝试使用通用 setInterval
、node模块,如 node-schedule、cron、node-cron、async-poll,但无法实现异步函数调用的轮询。
这是我在代码中尝试的:
cron.schedule("*/2 * * * *", await this.servicesManager.startPoll() => {
console.log('running on every two minutes');
}); // this is not working breaks after first run
const job = schedule.scheduleJob(" */1 * * * *", async function() {
try {
return await this.ServicesManager.startPoll(); // this function startPoll is undefined when using this
} catch (e) {
console.log(e);
}
console.log('Run on every minute');
});
const event = schedule.scheduleJob("*/2 * * * *", this.ServicesManager.startPoll()); //using node-schedule , breaks after first time
cron.schedule("*/2 * * * *", await this.ServicesManager.startPoll()); // using cron same result as using node-schedule
return await this.ServicesManager.startPoll(); // without polling works
计划的异步调用至少最多可使用
node-cron
,但我们可以使用v3.0.0
来使用node-schedule
,如下所示。JS
TS