Good Morning Everyone,
I'm having some trouble wrapping my head around some asynchronous activities.
What I'm looking to do is grab some data, pass the returned value to a variable, display that information (and do some other functions based on what comes out). This process should be expandable to multiple datasets, such as hourly forecasts, weekly forecasts, fire-hazard info etc. and will be running on an interval.
I've gone through MDN's Promise page along with numerous searching on here for an answer, but it doesn't seem to click.
What I have so far:
let weeklyData;
let pull;
async function pullData(url){
fetch(url).then(res=>res.json()).then((val)=>{pull=val});
}
function buildDataSets(){
for(let x = 0; x < pull.properties.periods.length; x++){
weeklyDataSets[x] = pull.properties.periods[x];
}
}
When called by hand in the console, these resolve correctly. Pull comes out as an object, and buildDataSets() can run. I imagine that's because there's enough time for pullData(url) to resolve.
I need to process the response from pullData(url) so I need to assign its return value to an object.
What I can tell, I should be using something in the ballpark of
Promise.resolve(pullData(url))
.then(()=> buildDataSets());
This returns a pending status, and pull remains undefined when buildDataSets() goes off. I know that I need to pull the data, wait until fulfilled, then assign the value into pull if successful. Then after pull has been assigned call the buildDataSets() function. Adding a .then() that calls buildDataSets() doesn't seem to work either resulting in pull to be undefined when it attempts to access values.
Would someone please help explain what I'm missing? I feel like I'm staring at the answer, but can't see it.