Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  124] [ 1]  / answers: 1 / hits: 85824  / 12 Years ago, fri, february 8, 2013, 12:00:00

I'm reading about Deferreds and Promises and keep coming across $.when.apply($, someArray). I'm a little unclear on what this does exactly, looking for an explanation that one line works exactly (not the entire code snippet). Here's some context:



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');
}

More From » jquery

 Answers
9

.apply is used to call a function with an array of arguments. It takes each element in the array, and uses each as a parameter to the function. .apply can also change the context (this) inside a function.



So, let's take $.when. It's used to say when all these promises are resolved... do something. It takes an infinite (variable) number of parameters.



In your case, you have an array of promises; you don't know how many parameters you're passing to $.when. Passing the array itself to $.when wouldn't work, because it expects its parameters to be promises, not an array.



That's where .apply comes in. It takes the array, and calls $.when with each element as a parameter (and makes sure the this is set to jQuery/$), so then it all works :-)


[#80342] Thursday, February 7, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dillionsalvadorg

Total Points: 288
Total Questions: 103
Total Answers: 75

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
dillionsalvadorg questions
;