我想安排一个异步函数(异步/等待返回类型)每两分钟运行一次。
我尝试使用通用setInterval
,节点模块如节点计划、cron、节点计划、异步轮询但无法实现异步函数调用的轮询。
这是我在代码中尝试过的:
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
尝试这样的事情
// version 1
cron.schedule("*/2 * * * *", this.servicesManager.startPoll);
// version 2 => if servicesManager needs its `this` reference
cron.schedule("*/2 * * * *", async () => this.servicesManager.startPoll());
//version 3 ==> using node-schedule
schedule.scheduleJob("*/1 * * * *", async () => this.ServicesManager.startPoll());
我不知道你的情况servicesManager
,您可能必须使用上面的“版本 2”才能使其正常工作。
调度库需要一个函数来执行,但在上面的示例中它们得到了一个已解决的 Promise。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)