0

I was trying to launch the exact same node.js script in my code via node-cron.

const cron = require("node-cron");
const Bot = require("./bot");

// Scheduled to run at 10:00 AM 
cron.schedule("0 10 * * *", () => {
   for(int I=0; I<5; I++){
     Bot();
   }
}, {
    scheduled: true,
    timezone: "America/New_York"
});

My script takes about 20 minutes to do its job and close. And the question is, will the cron written above launch my script 5 times simultaneously, or wait till previously launched one completes, then launch the next one? If it does wait, how can I make it launch simultaneously? Thank you beforehand!

A.Bokizh
  • 115
  • 1
  • 2
  • 8
  • Why don't you just try it? But it will launch your functions synchronously. And not sure what `int` is good for? – Getter Jetter Jan 10 '21 at 12:35
  • Yes, I forgot to remove int, thanks! The problem is, I'm not on this stage of production yet, but still, I want to kinda get my future code planned instead of leaving everything for the last minute. – A.Bokizh Jan 10 '21 at 12:41
  • you have to let us know the bot.js code. It depends on wheather the function Bot is an async function or not – Jun Jan 10 '21 at 12:45
  • @Jun I don't think this is correct. `Bot` will be called synchronously anyway. See [here](https://jsfiddle.net/4oey91cf/) – Getter Jetter Jan 10 '21 at 12:52
  • @Jun Bot is an async function and it contains awaits – A.Bokizh Jan 10 '21 at 12:56
  • @olivier-krull So, technically, my code should launch Bot() function simultaneously even if my function is async, right? – A.Bokizh Jan 10 '21 at 13:00
  • If your code is executed like shown in your snipped, yes. – Getter Jetter Jan 10 '21 at 13:03
  • Here another fiddle showing the behaviour https://jsfiddle.net/4oey91cf/1/ – Getter Jetter Jan 10 '21 at 13:12
  • @OlivierKrull asyncFunction contains setTimeout that is async function too. if asyncFunction didn't contains async function, It will be running orderly – Jun Jan 10 '21 at 13:13
  • @A.Bokizh If the function Bot is async, It would be running simultaneously – Jun Jan 10 '21 at 13:14
  • @Jun OP is asking if the second `Bot` in the loop will wait for the first one to be finished before executing. The answer is no. – Getter Jetter Jan 10 '21 at 13:17
  • Thank you so much guys!! I got it all settled now – A.Bokizh Jan 10 '21 at 13:43

0 Answers0