0

I have a piece of code that it works with all browsers except Internet Explorer and Opera.

Here is the code: JSFiddle

(async () => {
var InternalURL = "https://jsonplaceholder.typicode.com/posts/1";
var ExternalURL = "https://jsonplaceholder.typicode.com/comments";

    let image, data = await $.getJSON(InternalURL);
    if(data) {
        // Some code
    } else {
        // request failed
    }
    if(!image) {
        let data = await $.getJSON(ExternalURL);
        if(data) {
            // Some code
        } else {
            // request failed
        }
    }
    // Some code
})();

In IE in points to syntax error in line (async () => {

Any idea why is that and how to fix it?

As I noticed it is a ES6 code, is there anyway to convert a different code that is friendly with older browsers and still gives the same output?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
DannyBoy
  • 434
  • 5
  • 21
  • `async` keyword and `arrow function` ( `=>` ) is ES2017+ ... IE is a 1999 browser ... transpiler is your only option ... Opera should support => in version 32+ and `async` in version 42+ – Jaromanda X Oct 25 '17 at 05:45
  • [async support in browsers](https://caniuse.com/#feat=async-functions). You can use babel to compile your code for supporting older browsers. – E. Sundin Oct 25 '17 at 05:46
  • Opera is now based on Chromium and will work mostly as Chrome does, was it not updated in ages? – Walk Oct 25 '17 at 05:50

1 Answers1

0

async and await are not supported by IE, and are only supported in Opera since version 42 (see bottom of MDN article). Do not use ES6 with old browsers - use Babel, or another transpiler to transpile to ES5 (although possibly ES3 in the case of IE?).

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • If he wants to use promises rather than callbacks, he can use one of the available libraries, and convert the async/await code to a promise. I'm not sure what Babel transpiles `await` to for an old browser... – Traveling Tech Guy Oct 25 '17 at 05:50
  • Its duplicate... please follow this one: https://stackoverflow.com/questions/46066259/async-throwing-syntaxerror-unexpected-token – Kamal Oct 25 '17 at 05:51