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

158
Vistas
Javascript update variable value

Ahh I really didn't want to ask, but my brain is really frying about this now.

I have a function that polls information from database API:

function fetchDatabase() {
let fetchResult = database.fetch.local(something, apiHost("api.database.com"));
return fetchResult
}

The value of fetchResult that this function gets, needs to be updated every second. So I am putting 1 second interval on the whole function:

setInterval(fetchDatabase, 1000)

Now I need to be able to access this data as a variable >>> !!! OUTSIDE !!! <<< of the function so I write:

let outsideResult = await fetchDatabase();

I then verify my code with this:

console.log(outsideResult)

I am successfully getting output of "fetchResult" every second, but the problem is, that this value does NOT update as the database API from which I'm polling the "fetchResult" gets updated.

So I am getting the same values all the time and not updated ones.

Please, how can get updated result of the API database fetch every second?

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Define your variable outside of fetch function:

function fetchDatabase()
{
  let fetchResult;
  function loop() {
    fetchResult = database.fetch.local(something, apiHost("api.database.com"));
  }
  loop();
  setInterval(loop, 1000);
  return fetchResult;
}

let outsideResult = await fetchDatabase();
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