2

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
2
  • 1
    Why not transpile the code? Commented May 31, 2021 at 15:28
  • 1
    Also, you seem to only be asking about rest parameter syntax: function(...responses). Only $.when(...requests) is using spread. Commented May 31, 2021 at 15:30

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, it seems to work with a little tweak -> var responses = [].concat(arguments)[0];.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.