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

170
Vistas
Keep checking connection if available in Node and clear setTimeout

In node i keep checking if connection is available if it is it will get token and store in variable but if not then catch error will be thrown and it will then execute setTimeout to try again in 2 seconds.

But i think i need a clean way to clear the setTimeout because when i log it i keep getting higher number in Symbol(asyncId) this to me feels like it keeps adding setTimeout more and more to memory?

export const getToken = async () => {
  try {
    if (!GLOBAL_VARS.authToken) {
      await auth(`Getting token`)
    }

    // Do more stuff
  } catch (error: any) {
    tokenTimeout()
  }
}

getToken()
export const tokenTimeout = () => {
  setTimeout(() => {
    getToken()
  }, 2000)
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yest it will add more setTimeouts to the memory.

you can clear it like this:

export const tokenTimeout = setTimeout(() => {
    getToken()
  }, 2000)


export const getToken = async () => {
  try {
    if (!GLOBAL_VARS.authToken) {
      await auth(`Getting token`)
    }

    // Do more stuff
  } catch (error: any) {
    const timeout = tokenTimeout()
  }
}

getToken()

/// use it wherever you want 
clearTimeout(timeout);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Memory is going to increase because of whatever you are doing with auth() and not because you are creating timeouts.

The issue I see with your code is not really using async await properly. It should look more like

const sleep = (ms) => 
  new Promise((resolve) => {
    setTimeout(resolve, ms);
  });

export const getToken = async (count = 0) => {
  try {
    if (!GLOBAL_VARS.authToken) {
      await auth(`Getting token`);
    }
  } catch (error: any) {
    await sleep(2000);
    if (count < 10) await getToken(++count);
    else throw new Error('Taking too long to get auth token');
  }
}

(async function () {
  await getToken();
})();
about 4 years ago · Juan Pablo Isaza Denunciar
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