使用$ .when.apply&访问多个返回对象derferreds

时间:2013-03-12 12:41:51

标签: jquery

我有这样的代码,

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中的项目数是动态的。

1 个答案:

答案 0 :(得分:0)

你可以使用'special'参数参数

$.when.apply(this, deferredArray).then(function() { 
  for (var i = 0; i < arguments.length; i++) {
    console.log("Promise "+ i +" returned: " + arguments[i]);
  }
  alert("Finished"); 
});