when multiple ajax calls done

Nick Germi

Following is the scenario which doesn't work in the order I want:

var masterData = {};
var tableNames = ['table1','table2','table3','table4','table5','table6'];
var pullSqlData = function(){
  tableNames.forEach(function(value) {
    if(storage.isEmpty(value)) {
      $.getJSON('http://domain.com?r=appsync/read&id='+value+ "&callback=", function(data){
        masterData[value] = data;
        storage.set(value,data);
      });
    } else {
      masterData[value] = storage.get(value);
    }
  });
};

$.when(pullSqlData()).done(function(){
console.log('all done');
});

After searching around I know I can get above to work if I manually do something like

$.when(
$.getJSON('http://domain.com?r=appsync/read&id=table1&callback=', function(data){
        masterData[value] = data;
        storage.set(value,data);
      }),
$.getJSON('http://domain.com?r=appsync/read&id=table2&callback=', function(data){
        masterData[value] = data;
        storage.set(value,data);
      }),
//more calls
).done(function(){
console.log('all done');
});

However I was wondering if there is a way of doing above the proper way

*storage is a HTML5 localStorage jQuery plugin

Arun P Johny

Since you are a dynamic number of requests, try

var masterData = {};
var tableNames = ['table1', 'table2', 'table3', 'table4', 'table5', 'table6'];
var pullSqlData = function () {
    var requests = [];
    tableNames.forEach(function (value) {
        if (storage.isEmpty(value)) {
            var xhr = $.getJSON('http://domain.com?r=appsync/read&id=' + value + "&callback=", function (data) {
                masterData[value] = data;
                storage.set(value, data);
            });
            requests.push(xhr)
        } else {
            masterData[value] = storage.get(value);
        }
    });
    return requests;
};

$.when.apply($, pullSqlData()).done(function () {
    console.log('all done');
});

You need to pass all the ajax requests to $.when() so pullSqlData has to return the list of ajax requests created by it. Also $.when() does not take an array as an arguments so you need to use Function.apply() to pass the variable number of parameters to it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delay between multiple ajax calls using $.when().done in a loop

From Dev

Detecting When a Loop of AJAX Calls is Done

From Dev

Error when making multiple ajax calls

From Dev

AJAX multiple ajax calls

From Dev

Proper way to run multiple similar ajax calls with same done function, differing in input data

From Dev

Restrictions of Multiple AJAX Calls?

From Dev

Multiple Ajax Calls

From Dev

Waiting for multiple Ajax calls

From Dev

Multiple AJAX calls optimisations

From Dev

Prevent multiple ajax calls when re-using a controller/factory

From Dev

Multiple Ajax calls appear to be treated as synchronous requests when they should not be

From Dev

AJAX launch final asyc call when multiple asyc calls are complete

From Dev

Prevent multiple ajax calls when re-using a controller/factory

From Dev

Using ajax promise with $.when().done

From Dev

Using ajax promise with $.when().done

From Dev

When all ajax requests are done, then

From Dev

How to avoid multiple AJAX calls?

From Dev

Multiple Ajax Calls, One Callback

From Dev

show() overwritten multiple ajax calls

From Dev

How To Reduce Multiple Ajax Calls

From Dev

Multiple Ajax Calls, One Callback

From Dev

How to avoid multiple AJAX calls?

From Dev

AngularJS : multiple asynchronous AJAX calls

From Dev

call multiple ajax call with Jquery deferred When and then(3 parallel calls and then on complete one more ajax call)

From Dev

jQuery execute after 3 AJAX calls and loops are done

From Dev

How to call ".done" after the ajax call in Backbone fetch calls

From Dev

How to function with ajax calls asynchronously and display loader until done?

From Dev

jquery handling recursive ajax calls, how to tell the browser it's done

From Dev

jQuery ajax done() getting called multiple times

Related Related

  1. 1

    delay between multiple ajax calls using $.when().done in a loop

  2. 2

    Detecting When a Loop of AJAX Calls is Done

  3. 3

    Error when making multiple ajax calls

  4. 4

    AJAX multiple ajax calls

  5. 5

    Proper way to run multiple similar ajax calls with same done function, differing in input data

  6. 6

    Restrictions of Multiple AJAX Calls?

  7. 7

    Multiple Ajax Calls

  8. 8

    Waiting for multiple Ajax calls

  9. 9

    Multiple AJAX calls optimisations

  10. 10

    Prevent multiple ajax calls when re-using a controller/factory

  11. 11

    Multiple Ajax calls appear to be treated as synchronous requests when they should not be

  12. 12

    AJAX launch final asyc call when multiple asyc calls are complete

  13. 13

    Prevent multiple ajax calls when re-using a controller/factory

  14. 14

    Using ajax promise with $.when().done

  15. 15

    Using ajax promise with $.when().done

  16. 16

    When all ajax requests are done, then

  17. 17

    How to avoid multiple AJAX calls?

  18. 18

    Multiple Ajax Calls, One Callback

  19. 19

    show() overwritten multiple ajax calls

  20. 20

    How To Reduce Multiple Ajax Calls

  21. 21

    Multiple Ajax Calls, One Callback

  22. 22

    How to avoid multiple AJAX calls?

  23. 23

    AngularJS : multiple asynchronous AJAX calls

  24. 24

    call multiple ajax call with Jquery deferred When and then(3 parallel calls and then on complete one more ajax call)

  25. 25

    jQuery execute after 3 AJAX calls and loops are done

  26. 26

    How to call ".done" after the ajax call in Backbone fetch calls

  27. 27

    How to function with ajax calls asynchronously and display loader until done?

  28. 28

    jquery handling recursive ajax calls, how to tell the browser it's done

  29. 29

    jQuery ajax done() getting called multiple times

HotTag

Archive