0

我有这样的代码,

var deffereds = $(someArray).map(function(index, ele) {
  return $.Deferred(function(def) {
      //Do Some custom thing with ele here
      def.resolve(someValue); //SomeValue will be differnt for each element
  });
});
$.when.apply(null, deffereds.get()).then(function (r1,r2,...) { // <-- Here is the problem
   //How Can I access responses from all the deffereds?
});

我的问题是如何访问所有延迟的响应?因为 someArray 中的项目数是动态的。

4

1 回答 1

0

您可以使用“特殊”参数参数

$.when.apply(this, deferredArray).then(function() { 
  for (var i = 0; i < arguments.length; i++) {
    console.log("Promise "+ i +" returned: " + arguments[i]);
  }
  alert("Finished"); 
});
于 2014-02-14T08:51:54.527 回答