Reputation: 127
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.
Upvotes: 0
Views: 1018
Reputation: 10221
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();
Upvotes: 1