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