最佳答案
我是 reading about 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');
}