My problem is a weird one, as described in the title. Here's the code:
Case 1:
var first = $.ajax({ // about 500ms request
url: myUrl
success: function() { console.log(1); }
});
var second = $.ajax({ // about 200 ms request
url: myUrl
success: function() { console.log(2); }
});
$.when(first, second).done(function() { console.log(3); });
Logs 2, 1, 3. All good, just what I wanted.
Case 2:
var first = $.ajax({ // about 500ms request
url: myUrl
success: function() { console.log(1); }
});
var second = $.ajax({ // about 200 ms request
url: myUrl
success: function() { console.log(2); }
});
function logthree() {
console.log(3);
}
$.when(first, second).done(logthree());
Logs 3, 2, 1, which is a problem. The logthree() function should only execute once first and second resolve.
Why does this happen? How can I use Case 2 without any problems?
Note: same thing happens if first and second are functions and they return an $.ajax.
Note: same thing happens if first and second are both $.get.