相关疑难解决方法(0)

$ .when.apply($,someArray)做什么?

我正在阅读Deferreds and Promises并继续发表$.when.apply($, someArray).我有点不清楚它究竟是做什么的,寻找一条线正确工作的解释(而不是整个代码片段).这是一些背景:

var data = [1,2,3,4]; // the ids coming back from serviceA
var processItemsDeferred = [];

for(var i = 0; i < data.length; i++){
  processItemsDeferred.push(processItem(data[i]));
}

$.when.apply($, processItemsDeferred).then(everythingDone); 

function processItem(data) {
  var dfd = $.Deferred();
  console.log('called processItem');

  //in the real world, this would probably make an AJAX call.
  setTimeout(function() { dfd.resolve() }, 2000);    

  return dfd.promise();
}

function everythingDone(){
  console.log('processed all items');
}
Run Code Online (Sandbox Code Playgroud)

javascript jquery asynchronous promise

105
推荐指数
3
解决办法
7万
查看次数

标签 统计

asynchronous ×1

javascript ×1

jquery ×1

promise ×1