Simultaneous Promises Combined
JavaScript
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function doStuffLater(msg){
var def = $.Deferred();
setTimeout(function(){
def.resolve(msg);
}, getRandomInt(300, 1000));
return def.promise();
}
function needsBothReturns(one, two){
console.log({one:one, two:two});
}
var first = doStuffLater("Sent First");
var second = doStuffLater("Sent Second");
$.when(first, second).done(function(result1, result2){
needsBothReturns(result1, result2);
});