Is there a way to effectively handle the data returned from an async function?
example: JS FILE:
async function getData(){
try {
$.getJSON('./data.json', (data) => {
return data;
});
} catch(error) {
console.log("error" + error);
} finally {
console.log('done');
}
}
console.log(getData());
JSON FILE:
{
"stuff": {
"First": {
"FirstA": {
"year": [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017],
"Categories": ["Suspension", "Electrical", "Performance", "Motor"]
},
"FirstB": {
"year": [2007, 2008, 2009, 2010, 2011, 2012],
"Categories": ["Suspension", "Electrical", "Performance", "Motor"]
}
},
"Second": {
"SecondA": {
"year": [2002, 2003, 2004, 2005, 2006],
"Categories": ["Suspension", "Electrical", "Performance", "Motor"]
},
"SecondB": {
"year": [2007, 2008, 2009, 2010, 2011, 2012],
"Categories": ["Suspension", "Electrical", "Performance", "Motor"]
}
}
}
}
How can one get complete access to all the information in the JSON file and manipulate it accordingly? For instance, extracting "First" and "Second" and appending them to a div. How about "FirstA" and "FirstB", "SecondA" and "SecondB"... and so forth?
Currently, Promise {: undefined} is what I am getting.
Any assistance on this matter would be highly appreciated.
---------UPDATE---------
While running the console log within the function reveals the json data, the challenge is accessing the data externally.
Serge