2

I have an array of an unknown number of deferreds that I'm trying to resolve with $.when. When all the deferreds have been resolved, will the '.then' function give me back the resolved deferreds in the same order as I entered then in the array?

Pseudo code example:

var deferreds = [];
for(0,1,2,...,x) { 
   var def = JsonRpc(...);
   deferreds.push(def);
}
$.when.apply($, deferreds).then(function () {
  console.log(arguments.length); //outputs a number >= 0
  for(0,1,2,...,x) {
     console.log("Defered: ", arguments[0,1,2,...,x])
  }
});

Will deferreds[0] be equal to arguments[0] and deferreds[x] be equal to arguments[x]?

Thanks in advance for any and all replies! //Edvin

Edvin
  • 1,841
  • 4
  • 16
  • 23
  • I believe this is a duplicate question: http://stackoverflow.com/questions/26239821/jquery-when-multiple-ajax-requests-order-of-responses – meditari May 31 '16 at 13:53

2 Answers2

1

According to the documentation :

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(). For example:

var d1 = $.Deferred();
var d2 = $.Deferred();

$.when( d1, d2 ).done(function ( v1, v2 ) {
    console.log( v1 ); // "Fish"
    console.log( v2 ); // "Pizza"
});

d1.resolve( "Fish" );
d2.resolve( "Pizza" );

A little example :

var deferreds = [];
for (var i = 0; i < 3; i++) {
  var def = $.Deferred();
  deferreds.push(def);
}

$.when.apply($, deferreds).then(function() {
  console.log(arguments.length); //outputs a number >= 0
  for (var i = 0; i < 3; i++) {
    console.log("Defered: ", arguments[i]);
  }
});

deferreds[1].resolve("Fish2");
deferreds[2].resolve("Fish3");
deferreds[0].resolve("Fish1");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • Thanks for your reply! Very good example. I had already read that part of the documentation but I didn't know if it applied to arrays as well. I'm still quite new to Javascript so had to make sure :) – Edvin Jun 01 '16 at 06:44
0

Taking each question/statement one at a time ...

Does $.when resolve array of deferreds in the same order as they were in the array?

No. $.when() does not resolve the deferreds/promises passed to it. It aggregates deferreds/promises, each of which settles (resolves or rejects) by some other means. Here, "aggregate" means to accept a set of promises and return a single promise.

I have an array of an unknown number of deferreds that I'm trying to resolve with $.when.

Same again, $.when() doesn't resolve the deferreds/promises passed to it.

When all the deferreds have been resolved, will the '.then' function give me back the resolved deferreds in the same order as I entered then in the array?

No, .then() doesn't give back deferreds, resolved or otherwise. But yes, the arguments of its success callback function (if it fires) will be congruous with the original array.

Will deferreds[0] be equal to arguments[0] and deferreds[x] be equal to arguments[x]?

No, they won't be equal, but they will correspond, index-by-index. Each argument will be the result delivered by the corresponding deferred/promise in the original array.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44