-1
function runimage(){
  var vel = val.id;
  var secondlink = 'someurl'+vel+'stuff';                       

  $.getJSON(secondlink, function(beyta){
      var target = beyta.attachments.data[0].media.image.src;
      return target;
  });

  return "FISH";
}

I am making an API call via jQuery's getJSON method. The problem I am having is getting the runimage() function return the value of my target value.

The code return target gives me undefined. This is the result even if I place a dummy string in place of target.

To see if this is a problem with closures, I added a dummy return "FISH" outside of my getJSON call, and the code does indeed return "FISH"

How would I go about getting runimage() to return the value of variable target?

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Jason Chen
  • 2,487
  • 5
  • 25
  • 44

1 Answers1

1

The issue is that while you are waiting to get your AJAX response, the function has already returned "FISH".

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71