I've been working on this for a bit, and I feel like the solution has to be really obvious, but I can't figure it out.
I have a function(getImage) and inside of that function I have a getJSON function. I do some stuff to the data returned from the API call, and then return it, so the value that I return is the actual data that I want, but how do I get the parent function(getImage) to return that value also?
var getImage = function(urlforAPI) {
$.getJSON(urlforAPI, function(imageData) {
console.log(imageData);
for (x in imageData.query.pages) {
if (imageData.query.pages[x].hasOwnProperty("thumbnail")) {
var storyImage = "<img src=" + imageData.query.pages[x].thumbnail.source + ">";
} else {
var storyImage = "No image.";
}
}// End FOR loop
console.log(storyImage); //This is logging the data I need
return storyImage;
})//End of getJSON
//What do I return here to get the same value that the child function returned?
}// End getImage
If it matters, I will be calling this function inside of another function and storing it like var tempImage = getImage(theURL)
. The param I'm passing it is generated inside of the function that I'm calling it in. I will use tempImage
to output the value of getImage
to the DOM.
How do I get the value returned from the getJSON child function to return from getImage
when it is called?
I read this javascript child function pass the return to the parent function return but I don't exactly understand what the callback(true)
/callback(false)
bit is doing.