I'm looking for a little advice regarding the best way to execute multiple ajax calls and then combine the results. My problem is that according to the documentation of the when function it will map the resulting argument differently when there are multiple arguments vs just a single argument. https://api.jquery.com/jquery.when/
This resulted in code that looks like the following. The check seems ugly to me and as far as I can tell would need to be done everytime $.when.apply is used. Is there a way to pass in an array of deferreds to the when function so that the output is of a consistent shape?
//Returns an array of promises that are simple ajax get requests
var getEventFramesPromises = self.webServiceAdapter.getEventFrames(distinctEventFramePaths);
$.when.apply($, getEventFramesPromises).then(function () {
var eventFrames;
//Now "arguments" will either be an array with three arguments it the length of the getEventFramesPromises ===1
if (getEventFramesPromises.length === 1) {
eventFrames = [arguments[0]];
} else {
//Or it will be an array of arrays where each item in the array represents a deferred result
eventFrames = _.map(arguments, function (args) {
return args[0];
});
}});