我是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');
}
'.done()'來代替.then'的'被用來在這種情況下,只是FYI – 2013-02-08 16:39:53
FWIW,有一個遞延端口強調,允許通過一個單一的array to'_.when',所以你不需要使用'apply' – Eevee 2013-02-08 16:45:12
瞭解更多關於'.apply'的信息:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function /應用。 – 2013-02-08 16:48:38