Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda