0
var array = [DefferdObj, DefferdObj, DefferdObj, DefferdObj];

array[0].then
array[1].then
array[2].then
  ・
  ・
  ・

I want to execute DefferedObj asynchronously. This array can be changed dinamically.

hoshipeace
  • 51
  • 6
  • 1
    That makes no sense! Do the objects represent an asyncronous operation, like a function containing an ajax call that reeturns a promise etc, if not, how do you intend to get async behaviour, and does the array contain just promises, or a reference to a function that you can call. You need to add more code! – adeneo Jun 17 '13 at 07:21
  • Hoshipeace, if you have an array of Deferreds (or promises), then it is too late to determine the sequence of execution that gave rise to these Deferreds. Now, if the array was to be a sequence of *functions*, that would be a different matter. – Beetroot-Beetroot Jun 17 '13 at 07:40
  • Thank you for reading my problem! I try to write better question next time. – hoshipeace Jun 20 '13 at 11:38

1 Answers1

0

If you are looking to run a piece of code after they all complete then:

$.when.apply(null, array).then(function() {
  console.log("all deferreds in array are complete!");
});

If you are looking to control the order the deferreds run, you will have to be sure the deferreds haven't already started. Before you put them in the array, I would suggest wrapping them in an ongoing deferred.

var last = array[0];
function tackOnFunction(fn) {
  array.push(last = last.then(fn));
}
jcbelanger
  • 539
  • 3
  • 15