I'm trying to push some Javascript functions into an array to call them with jQuery.when() but when doing so, the functions are already called when put into the array. Here's an example and here's my code snippet:
var answers = someJSONFormattedData;
var calls = [];
function callWithIndex(i) {
var answer = answers[i];
api.loadUser(answer.userType, answer.userId, function(userData) {
answer.user = userData;
}, null)
};
for(var i=0;i<answers.length;++i) {
calls.push(callWithIndex(i));
}
$.when.apply($, calls).then(function() {
var dataAsJSON = {
'answers': answers
};
//do some magic stuff with dataAsJSON
});
Even when doing a short test code snippet, the test() function is called immediately although I wouldn't it to be called at all as I only put it into an array (or at least try to).
var test = function(i) {
console.log("test: "+i);
}
var testArray = [];
for(var i=0;i<5;++i) {
testArray.push(test(i));
}