Below is the code I am trying to convert to ES5 for IE compatibility.
$.when(...requests).then(function(...responses) {
// More code goes here
}
So far I could convert it to the below and it works well, but I still couldn't replace the then part.
$.when.apply(null, requests).then(function(...responses) {
// More code goes here
}
Any suggestion would be really appreciated.
asked May 31, 2021 at 15:17
Parag Kadam
3,8905 gold badges27 silver badges58 bronze badges
1 Answer 1
There is arguments:
$.when.apply(null, requests).then(function() {
var responses = [].concat.apply([], arguments); // Get a standard array from arguments object
// More code goes here
}
As a disclaimer I quote MDN on the use of arguments:
If you're writing ES6 compatible code, then rest parameters should be preferred.
So when you don't have ES6 support (which seems to be your case), then using arguments is fine, but otherwise you should use the spread syntax in the function parameter list.
answered May 31, 2021 at 15:25
trincot
357k38 gold badges282 silver badges339 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Parag Kadam
Thanks for the answer, it seems to work with a little tweak ->
var responses = [].concat(arguments)[0];.trincot
Indeed. I corrected this in the answer. It should in fact use
apply, otherwise there is no conversion to standard Array. But that conversion is optional. If you don't use Array methods like .forEach, ...etc, you can just use responses = arguments or use arguments directly.lang-js
function(...responses). Only$.when(...requests)is using spread.