I am trying to learn node.js after python and trying to thread an infinitely running process in the background with a 2 minute sleep().
main.js
const TeleBot = require("telebot");
const fun = require('fun.js');
fun(); //function I want to thread
const bot = new TeleBot({
token: "Bot_Token",
});
bot.on(["/start", "/hello"], (msg) => {
bot.sendMessage(msg.from.id, `Hello ${msg.chat.username}`);
});
bot.start();
I saw some people using this sleep() function but it didn't work
fun.js
module.exports = async function fun()
{
while (1) {
await sleep(10000);
console.log("I ran");
}
}
Error: sleep is not defined
So, I tried this
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
module.exports = async function fun()
{
while (1) {
await sleep(10000);
console.log("I ran");
}
}
It works but is this a good practice and ok to use? Also, can someone tell me how to use worker-threads for this.