0

I'm using a jQuery JSON function inside another function, how can I return an array made in the jQuery function as the return value of my parent function?

This is the basic setup:

function getFlickrSet(flickr_photoset_id){
  var images = [];
  
  images = $.getJSON(url, function(data){ return data; // I HAVE THE DATA HERE };
  
  return images // I HAVE NO DATA HERE
}
var myImages = getFlickrSet(23409823423423);
alert(myImages); // this gives me nothing

I have set up an example on JS Fiddle. Where is my code incorrect?

2 Answers 2

4

You can't. Instead, pass in a function:

function getFlickrSet(flickr_photoset_id, when_ready){
  var images = [];

  $.getJSON(url, function(data){ 
    // prepare images
    when_ready( images );

  });
}

getFlickrSet(nnnn, function(images) {
  alert(images);
});

Why can't you do that? Because the "$.getJSON()" call is asynchronous. By the time that the callback function is called (where you wrote, "I HAVE THE DATA HERE"), the outer function has returned already. You can't make the browser wait for that call to complete, so instead you design the API such that code can be passed in and run later when the result is available.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for telling me whats going on, as i'm new to javascript, could you please kindly look at the jsfiddle example and show me how this would be implemented. I would greatly appreciate it. thank you!
1

Well, Ajax is asynchronous (that's what the 'A' stands for), so you must do this in an asynchronous way, which boils down to callbacks. What you need to do is pass a callback function to your outer function that you want to be called ("called back," if you will) when the Ajax request completes. You could just give it 'alert' like this:

function getFlickrSet(flickr_photoset_id) {
  images = $.getJSON(url, alert); // <-- just the name of the function, no ()
}
var myImages = getFlickrSet(23409823423423);
// => An alert pops up with the data!

...but more likely you'd write something like this:

function doSomethingWithData(data) { // we'll use this later
  alert(data); // or whatever you want
}

function getFlickrSet(flickr_photoset_id, callback) {
  // new parameter here for a function ------^
  // to be given here -------v
  images = $.getJSON(url, callback);

  return images // I HAVE NO DATA HERE
}

var myImages = getFlickrSet(23409823423423, doSomethingWithData);
// => Your function `doSomethingWithData` will be called the data as a parameter
//    when the $.getJSON request returns.

4 Comments

Thanks Jordan, that shed some light on what is going on, seeing that I'm new to this language, if you could see where i've gone in implementing your solution I would greatly appreciate your time. jsfiddle.net/taheri/6DQuK/3
@Mohammad no, that's not going to work. You can return things from the AJAX handler function, but jQuery has no place to put it so it's just thrown away. You have to do it as Jordan and I have written above.
I was wondering, is there any way I can pass an argument to the call back function "doSomethingWithData" other than data?
found the answer over here : ) stackoverflow.com/questions/3430380/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.