2

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.

Community
  • 1
  • 1
Chirpizard
  • 291
  • 3
  • 17

1 Answers1

1

Initialize the variable in the first function, set it in the nested function, then return it.

var storyImage = "";
$.getJSON(urlforAPI, function(imageData) {
  console.log(imageData);
  for (x in imageData.query.pages) {
    if (imageData.query.pages[x].hasOwnProperty("thumbnail")) {
      storyImage = "<img src=" + imageData.query.pages[x].thumbnail.source + ">";
    } else {
      storyImage = "No image.";
    }
  }// End FOR loop
  console.log(storyImage); //This is logging the data I need
})//End of getJSON
rhinosforhire
  • 1,305
  • 11
  • 20
  • Ok, I just tried this. When I return ```myResult``` in the parent function and then later call the parent function, it returns an object instead of the string that the child function returns. It returns ```Object: { readyState: 1 }``` when I log myResult right before the return statement. – Chirpizard Dec 14 '15 at 22:12
  • Ok, I tried the new solution. When I return from the parent function I just get the empty string. If I return from the child I'm getting the desired value(either the URL returned from the API call, or "No Image" as the else statement). – Chirpizard Dec 14 '15 at 22:44
  • Did you add `return storyImage;` to the end of the outer function? – rhinosforhire Dec 14 '15 at 22:47
  • Yes. I also tried returning it in the child. Here is the pen on Codepen. Click the random button you are served with a story, in the console log you'll see 2 logs. The first will have a URL for an image IF that story has an image, or it will say "No Image.". The second log will be blank, that is the log from the parent function. It's just logging the initial set value of storyImage. http://codepen.io/Chirpizard/pen/objyKv?editors=001 – Chirpizard Dec 14 '15 at 22:52
  • Actually it seems like it is logging the result of the parent function first and then the child function, thats weird. Why is that? – Chirpizard Dec 14 '15 at 22:53
  • 1
    I found the answer here: http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text – Chirpizard Dec 14 '15 at 23:25
  • Thanks for your time! – Chirpizard Dec 14 '15 at 23:25
  • Oooooooh, so you use `callBack(result);` because it's an AJAX function! That's great to know, thanks. P.S. I couldn't see the debug tab, to view the console, on the codepen as a free user :\ – rhinosforhire Dec 14 '15 at 23:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97909/discussion-between-chirpizard-and-rhono). – Chirpizard Dec 14 '15 at 23:43