Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!