I want to make a GET request to an API and retrieve data from it. From what I know, my $.getJSON
is asynchronous, meaning it won't wait till it finishes before it continues with my code. Right now, I think the best route I can tell from research is to use a callback, but I'm still confused about how best to implement this (if even the best way). Can you use this example code below to demonstrate?
function getData(URL) {
$.getJSON(URL, function(data) {
//play with data, such as save to localStorage on web broswer
}
}
function container() {
var URL = "https://someurl....";
getData(URL);
// Now access the data from localStorage, which was saved during .getJSON function, will fail if getData is ran async due to delay
// Open URL here appending data from the GET request, have to obviously have it prior to proceeding
}
I know this is fairly wide discussed and I assure you I didn't post this before researching, but I'm still having a hard time. If someone could demonstrate with this example, it might really help me understand. Thank you!