1

I'm trying to use jQuery's when() and pass in a dynamic list of XHRRequests, but apparently when() does not support an array as its parameter.

How would I go about doing this?

I would really appreciate any help, thanks!

AndyHsiung
  • 43
  • 6
  • possible duplicate of [jQuery $.when() with variable arguments](http://stackoverflow.com/questions/8011652/jquery-when-with-variable-arguments) – Felix Kling Mar 01 '13 at 00:11

1 Answers1

2

The key function here is apply().

$.when.apply(null, arrayXHR).done(function(response1, response2, ....){ 
    console.log(response1, response2 ...);
});
AlexCheuk
  • 5,595
  • 6
  • 30
  • 35
  • 1
    Any reason why you do not directly pass `callback` to `.done`: `$.when.apply(null, arrayXHR).done(callback)`? Or even better, return the promise to allow the calling code to attach the callbacks: `return $.when.apply($.when, arrayXHR);`. – Felix Kling Mar 01 '13 at 00:12