$.when.apply($, someArray) 有什么作用?
2022-08-30 05:19:52
我正在阅读有关延期和承诺的信息,并不断遇到.我有点不清楚这到底是做什么的,寻找一个解释,一行完全有效(而不是整个代码片段)。以下是一些上下文:$.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');
}