I am trying to make an ajax call for each item in an array. Right now i throw all the promises into an array and then do $.when.apply...
// throw all the promises into an array
_.each(birds,function(bird,i){
birds[i] = getBird(bird) // getBbird returns $.ajax(...)
});
// do $.when.apply
return $.when.apply($,birds).then(function(res){
console.log("bird is the word",res)
});
My initial SO search basically confirmed I am doing this "the way" it should be done. But apply feels so hacky. Is there a more standardized/common jQuery way to achieve this?
Thanks in advance.
applyis not hacky. It is good JS!$.when.apply($is the equivalent to$.when(, so that's the safer option..whenAll()method or allow$.when()to accept an array. Internally such methods would use.apply(), as do many other plugins. In the plugin world, an understanding of.apply()and.call()is pretty well mandatory.$.when.apply(whatever,...appears to be the quiv of$.when(...