我正在阅读有关 Deferreds 和 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');
}