小编csl*_*rnr的帖子

在 NodeJS 中调度异步函数

我想安排一个异步函数(async/await ruturn 类型)每两分钟运行一次。

我尝试使用通用setInterval节点模块,如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 * * …
Run Code Online (Sandbox Code Playgroud)

javascript cron node.js async-await

5
推荐指数
1
解决办法
7913
查看次数

标签 统计

async-await ×1

cron ×1

javascript ×1

node.js ×1