Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda