20

I want to send [1, n) AJAX-requests to the server and after all have returned a result a modal dialog should be closed. $.when(a(), b(), c()) would be perfect, but I don't know how to pass the variable count of functions to $.when as parameter. Any ideas how to solve this problem?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Botic
  • 203
  • 2
  • 5
  • related: [How do you work with an array of jQuery Deferreds?](http://stackoverflow.com/q/4878887/1048572) – Bergi Jul 10 '14 at 23:27

1 Answers1

28

Call the functions and add their return values to an array. Then call $.when passing the array as argument like so:

$.when.apply($, array)

See Function.prototype.apply [MDN] for more information and my answer here for an extended example.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Sometimes it's just time to knock one's head against the table... JS is too beautiful to get all of it's beauty – .apply() is one of it! Thanks a lot for the "Denkanstoß" ;-) – Botic Nov 04 '11 at 15:35