Search code examples
reactjsasync-awaitinternet-explorer-11

React IE11 async causes Expected } error


I'm trying a tutorial for a basic react web app and it works perfectly in chrome. Here is my code:

import React, {Component} from 'react';

export default class App extends Component {
  async componentDidMount() {
    const [r1,r2] = await Promise.all([
        fetch('http://api.mywebsite.com/content/cars'),
        fetch('http://api.mywebsite.com/content/aircrafts'),
    ]);
    this.setState({content:await r1.json(),content2: await r2.json()});

  }
  render() {
    if(this.state) {
      console.log(this.state);
    }
    return (
        <div>hello</div>
    )
  }
}

This works exactly as I expect in Chrome - load up the react component, fetch some data, set the state, and print the state to console.

However, this code will not run in Internet Explorer 11. All I get is a blank page, and in my developer tools, I get the error

Expected '}'

When I click on the link to the error, it highlights this segment of code:

  _createClass(App, [{
    key: 'componentDidMount',
    value: async function componentDidMount() {
      var _ref = await Promise.all([fetch('http://new.evermight.com/content/index'), fetch('http://new.evermight.com/content/index')]),
          _ref2 = _slicedToArray(_ref, 2),

With an arrow pointing to the line value: async function componentDidMount() {.

How do I make this work in Internet Explorer 11? I only want setState to fire after the fetch calls and such are complete. I need the await behaviour.


EDIT

I am using webpack to compile my project. And if it helps, here is my package.json file:

{
  "name": "scroll",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "animated": "^0.2.1",
    "es2015": "0.0.0",
    "gsap": "^1.20.3",
    "history": "^4.7.2",
    "jquery": "^3.2.1",
    "react": "^16.1.1",
    "react-dom": "^16.1.1",
    "react-ga": "^2.4.1",
    "react-router-dom": "^4.2.2",
    "react-scripts": "0.9.5",
    "react-transition-group": "^1.2.0",
    "scrollmagic": "^2.0.5",
    "video-element": "^1.0.3"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.17",
    "css-loader": "^0.25.0",
    "eslint": "^4.13.0",
    "eslint-config-aqua": "^2.0.1",
    "eslint-plugin-react": "^7.5.1",
    "file-loader": "^0.9.0",
    "node-sass": "^3.10.1",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "uglifyjs-webpack-plugin": "^1.1.6",
    "url-loader": "^0.5.7",
    "webpack": "^3.8.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

And here is my webpack.config.js

var webpack = require('webpack');
var CleanPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: {app:'./src/Index.js'},

  output: {
    filename: '[name].bundle.js',
    chunkFilename: '[id].[hash].bundle.js',
  path: '/var/www/html/public/build',
    publicPath: '/build/'
  },
  plugins: [
        // This plugin minifies all the Javascript code of the final bundle

        new UglifyJsPlugin({
          uglifyOptions:{
            mangle:   true,
            compress: {
                warnings: false, // Suppress uglification warnings
            },
          }
        }),

        new webpack.optimize.CommonsChunkPlugin({
            name:      'main', // Move dependencies to our main file
            children:  true, // Look for common dependencies in all children,
            minChunks: 2, // How many times a dependency must come up before being extracted
        })
  ],
  module: {
    loaders: [
      { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' },
      { test: /\.scss$/, loaders: [ 'style-loader', 'css-loader', 'sass-loader' ]},
      { test: /\.(jpg|gif|png|otf|eot|woff|svg|ttf)(\?.*)?$/, loader: "file-loader" }
    ]
  }
}

Solution

  • Actually, I did get async and await to work in IE11. In my question, I also had a problem with fetch not being supported in IE11. Here's what I did to solve both problems. I went to my bash terminal and typed this

    npm install --save es6-promise
    npm install --save-dev babel-polyfill
    npm install --save-dev babel-plugin-transform-async-to-generator
    npm install --save isomorphic-fetch
    

    Next, I added these two lines to the very beginning of my src/Index.js before any other code because it is my entry point:

    import "babel-polyfill";
    import "isomorphic-fetch";
    

    Then I ran the webpack command, and now my IE11 supports the code I created with async await and it supports the fetch command.