如何终止正在执行的异步函数

时间:2019-08-30 02:18:51

标签: javascript node.js puppeteer

我有一个使用Puppeteer在前台运行的Web抓取程序。我需要杀死一个异步函数puppeteer(不是NODE.JS PROCESS)。为什么我要杀死它?因为此Web抓取需要每天执行一次,并且我想杀死它只是为了防止由于某种原因(如果仍在运行)同一功能被执行多次。

这是我的代码:


class Webscraping {

   start(){
      this.scrappeWithPuppeteer()
      this.runPuppeteerAgainDaily()
   }

   scrappeWithPuppeteer() {
     // asynchronous code
   }

   runPuppeteerAgainDaily() {
      // I'm usingthe npm package "node-schedule"
      schedule.scheduleJob('5 52 21 * * *', () => {
         // KILL CURRENT this.scrappeWithPuppeteer()
         // and then
         this.scrappeWithPuppeteer()
      }) 
   }


}

let webscrapper = new Webscraping()
webscraping.start()

1 个答案:

答案 0 :(得分:2)

schedule.scheduleJob是 node-schedule NPM模块的一部分。

如果您查看文档,https://www.npmjs.com/package/node-schedule

您可以看到以下代码取消了预定的作业:

job.cancel();

换句话说,请跟踪您的工作,以便以后可以通过上述功能将其取消。

相关问题