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

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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