JavaScript Multiple get requests jquery multiple ajax requests jquery,multiple http requests jquery,prevent multiple

I'm trying to read in several urls as text and store them into an array with jquery's ajax/get. I need to wait for all data to be obtained (and pushed to an array) and then return it in a function. From what I read online, this isn't possible.

I figured if I set a global array variable and pushed into that every time new data is obtained, then checked using a while loop if the array is saturated, when it is return. See below

You can assume all errors are handled within the get call

function fetchData(){
   x = [1,2,3,4,5,6,7,8,9,10];
   a = [];

   //Loop, get and push data
   $.each(x, function( i, val ) {
       $.get("http://someurl/"+i, function( data ) {
           a.push(data);
       });
   });

   // Wait till our array is populated?
   while(a.length < x.length){
       console.log(a.length);
   }
   return a;
}

However, this approach does not seem to be working and nothing is ever added to the array.. Can someone point me in the right direction?

Thank you

Answer:1

The solution you are looking for is to use a callback

function fetchData(callback) {
    var requests = [];
    for (var i = 1; i <= 10; i++) {
        requests.push($.get("http://someurl/" + i))
    }

    $.when.apply($, requests).then(function () {
        var array = $.map(arguments, function (arg) {
            return arg[0];
        });
        callback(array);
    })
}

fetchData(function (array) {
    //do something with array
})

PoC: Fiddle

Answer:2

issue 1

Here you are doing console.log on the result array even before the callbacks get fired. So the array a will always be empty. What you want to do is to trigger the console.log after all the values in array x are processed.

issue 2

You are trying to return the result array back. Instead you should try getting the code that processes a inside the callback.

like

function fetchData(cb){
   x = [1,2,3,4,5,6,7,8,9,10];
   a = [];

   //Loop, get and push data
   $.each(x, function( i, val ) {
       $.get("http://someurl/"+i, function( data ) {
           a.push(data);

           if(a.length == x.length) {
                //looping logic taken inside callback
                while(a.length < x.length){
                   console.log(a.length);

               }
               //calling the callback on result
               cb(a);
           }

       });
   });  

}

fetchData(function(data){

    //process a here

});
Answer:3

Say I have the following HTML code (it's an example): <div id="site_header_container" class="site_bar_container"> <div id="site_header"> <div id="header_logo_container"> ...

Say I have the following HTML code (it's an example): <div id="site_header_container" class="site_bar_container"> <div id="site_header"> <div id="header_logo_container"> ...

  1. position element relative to another
  2. position element relative to parent
  3. position element relative to viewport
  4. position element relative to sibling
  5. position element relative to another css
  6. position element relative to window
  7. position element relative to parent div
  8. position element relative to screen
  9. position element relative to browser window
  10. position element relative to another javascript
  11. position element relative to another jquery
  12. position element relative to grandparent
  13. position element relative to page
  14. position element relative to other element
  15. position element relative to viewport css
  16. css position element relative to sibling
  17. html position element relative to another
  18. css position element relative to window
  19. css position element relative to screen
  20. react position element relative to another

I'm familiar with using jQuery's $.ajax: $.ajax({ url: //twitter endpoint, method:"GET", dataType:"jsonp", success:function() { //stuff } }); How can I specify the JSONP datatype to an ...

I'm familiar with using jQuery's $.ajax: $.ajax({ url: //twitter endpoint, method:"GET", dataType:"jsonp", success:function() { //stuff } }); How can I specify the JSONP datatype to an ...

  1. json specify data type

I have a small JavaScript graphing/histogramming library which allows zooming/panning in either X or Y directions, or both, as declared when the plot is created. A simplified example is in this ...

I have a small JavaScript graphing/histogramming library which allows zooming/panning in either X or Y directions, or both, as declared when the plot is created. A simplified example is in this ...

I want to add a transition time to my pure javascript toggle class function I knew how to code it but i forgot and now need some advice <!DOCTYPE html> <html> <head> <style ...

I want to add a transition time to my pure javascript toggle class function I knew how to code it but i forgot and now need some advice <!DOCTYPE html> <html> <head> <style ...