0

I have referred the following links

Multiple ajax calls inside a each() function.. then do something once ALL of them are finished?

Coordinating multiple ajax requests with jquery.when

I want to use jquery.when method in foreach loop. So want to know about parameters in the following code

 $.when.apply($, calls).then(function(call1, call2, call3) {

    });

Suppose var calls = []; is a array of 3 get ajax calls. So in above is it necessary to provide 3 parameters like call1, call2, call3 or all these 3 calls can be combined into one parameter call as follows?

$.when.apply($, calls).then(function(call) {

        });
Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124
  • 1
    [`$.when()`](https://api.jquery.com): _"...If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. **The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when()**"_ -> One argument per deferred (in `calls`). – Andreas Feb 03 '18 at 13:29
  • @Andreas Thanks for reply. Yes, the arguments passed to the doneCallbacks provide the resolved values. So for resolved values, number of arguments passed to the doneCallbacks should match with the number of deferreds? or we can pass only single argument to doneCallbacks for all deferreds? – Shailesh Jaiswal Feb 03 '18 at 13:48
  • Only if you change the way jQuery is implemented/works – Andreas Feb 03 '18 at 13:48
  • @Andreas So it means, if there are 3 get ajax calls(in $.when.apply($, calls), then we need to pass three arguments to doneCallbacks (like .then(function(call1, call2, call3)). We can not pass single argument to doneCallbacks (like .then(function(call)) in case of above scenario – Shailesh Jaiswal Feb 03 '18 at 14:03
  • You don't have to use any of the arguments. If you're not interested in the result of any of the calls, you can go with `.then(function() { ... })`. It's up to you... – Andreas Feb 03 '18 at 14:04
  • @Andreas I am interested in result. So I need to pass arguments. Also in my scenario, number of ajax calls in when method depends on dynamic value. I am calling when method in for loop. Sometimes loop may iterate 3 time, 4 times or any number of times. Therefore I am concerned with number of arguments to doneCallbacks – Shailesh Jaiswal Feb 03 '18 at 14:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164463/discussion-between-shailesh-jaiswal-and-andreas). – Shailesh Jaiswal Feb 03 '18 at 14:14

1 Answers1

0
const requests = [
  $.get("test.json").then(function() { return "get"; }),
  $.getJSON("test.json").then(function() { return "getJSON"; }),
  $.ajax({ url: "test.json", method: "get", dataType: "JSON"}).then(function() { return "ajax"; })
]

$.when.apply($, requests)
  .then(function() {
    for (var i = 0, l = arguments.length; i < l; i++) {
      document.body.insertAdjacentHTML("beforeend", "<p>" + arguments[i] + "</p>");
    }
  });

The arguments to doneCallbacks should be passed as per above example if number of Deferreds were passed to jQuery.when() are dynamic. Thanks to @Andreas for his valuable guidance

Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124