Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
how to use threads and sleep() functionality with node.js

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!