I has an array of ajax requests:
let requests = urls.map(function(url){
return $.ajax({url: url, dataType: 'json'});
});
and I want to process them on. I am using when
:
$.when(...requests).then(function(...responses){
let processed = responses.map(function(responseRaw, index){
let response = responseRaw[0];
return /*some processed request*/;
});
//do something else
}).fail(function(error){
//process errors
});
jquery returns responseRaw
: it is array-like object containing data, status and something else. This works fine with several requests, but fails with one request: instead of one argument function then expects a three argument function (responseRaw spreaded). This is not a spread operator problem, but jquery one.
How to avoid that? My workaround:
$.when(...requests).then(function(){
let responses;
if (arguments.length == 3 && arguments[1] == "success"){
responses = [arguments];
} else {
responses = Array.from(arguments);
}
(BTW, is there a more clean way to get data instead of responseRaw[0]
?)