0

I have a following javascript program:

                function jQueryFunction(url, callback)
                { 
                    $.ajax
                            ({
                                type: "GET",
                                async: true,
                                url: url,
                                dataType: "jsonp",
                                jsonp: "callback",
                                jsonpCallback: "tpsHandler",
                                success: function(json)
                                {
                                    return callback(json);
                                }
                            });  
                }

                var jsonArray = new Array();
                for(var i = 0; i < 10; i++)
                {
                    jQueryFunction(url[i], function(json){
                       jsonArray[i] = json;
                    });
                }

                //process jsonArray

However, when I check jsonArray after the for loop, it is null. So my question is that how to store the return value from jQueryFunction to jsonArray in for loop and then process it?

I have tried $.when.apply($,jsonArray).done(function) but still the same, it is null.

user2597504
  • 1,503
  • 3
  • 23
  • 32

2 Answers2

1

A simple way:

function doTheAjax(url, callback) { 
  return $.ajax({
    type: "GET",
    async: true,
    url: url,
    dataType: "jsonp",
    jsonp: "callback",
    jsonpCallback: "tpsHandler"
  });
};

var reqs = [];

for(var i = 0; i < 10; i++) {
  reqs.push(doTheAjax(url[i]));
}

// send the array of requests to when, which will fire `done`
// when it's, well, done
$.when.apply($.when, reqs).done(function() {
  $.each(arguments, function(data) {
    // process the data
  });
});

alternatively, make a bunch of requests and put them into jsonArray, but keep track of how many you're making. When they all complete, you have the array. Create your own deferred, resolve it yourself when the counter is up, and return the promise.

var getJSON = function(url) {
  var dfd = $.Deferred();
  var count = 0;
  var total = 10;

  var jsonArray = [];

  for(var i = 0; i < total; i++) {
    doTheAjax(url[i]).done(function(json) {
      jsonArray.push(json);
      count++;
      if ( count >= total ) {
        dfd.resolve(jsonArray);
      }
    });
  }
  return dfd.promise();
};

getJSON().done(function(theCreatedJsonArray) {
  // do stuff
});
Dan Heberden
  • 10,990
  • 3
  • 33
  • 29
0

I'm not sure why the answer to your previous question (using deferreds) didn't work. But the cause of your problem is that you are checking the array before any of the ajax responses arrived. You also have a problem with i referencing the same value on all callbacks.

One simple workaround, if you know how many responses you're expecting:

var arr = [];
for(var i = 0; i < 10; i++){
    jQueryFunction(url[i], function(json){
        arr.push(json);
        if(arr.length == 10) {
            // All 10 responses arrived!
            // DO STUFF FROM HERE
            // e.g., call another function
            console.log(arr);
        } 
    });
}
Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150