So what I am trying to do is basically store the json that I get from the provided url into the global variable dataSet. Now the problem is the console.log() method at the end of this snippet tells me that dataSet is undefined. And it doesn’t matter if I store the returned json directly into the variable or via storeJSON(). In both times console.log() returns undefined. Why is that ? When I call console.log() from within storeJSON() I get the json printed to the console.
From your snippet, what purpose does the below function serve?
const storeJSON = (data) => {
dataSet = data;
}
for your problem, check what order things are happening by putting a console log in your fetch and see which console log entry comes first. I don’t know how you’re calling the fetch based on the code you provided
That’s because the console.log is executed before the fetch has completed and thus dataSet hasn’t been set to data when the console.log does its thing. Remember, fetch is asynchronous, so you can’t count on it being completed before you get to the console.log. If you want to log the value of dataSet after the fetch has completed you need to do it in the then callback where you are setting its value.
Hey that’s just a function that I executed within the 2nd then.() callback. I was trying to see if storing the dataset with a function would do me any favor. Sorry for the late reply.