1

So I'm trying to make a function of a class I made async, but webpack gives me an error when compiling the class.

My code would be something like this:

class MyClass {

   constructor(apiService) {
       this._apiService = apiService;
   }

   async updateInformation() {
       await this._apiService.updateInformation();
       // .. do more stuff I have to do 
   }
}

The error that webpack gives me is:

Module build failed: SyntaxError: Unexpected token

(The Unexpected token points to the 'u' after async)

armellini13
  • 377
  • 1
  • 3
  • 11
  • what is your bundling/transpiler setup? babel6, webpack... – Davin Tryon Apr 25 '16 at 17:33
  • 1
    `async/await` is not part of ES6, so it won't run in environments that (only) support ES6. You need to convert it do ES6 code first (e.g. using Babel or regenerator). – Felix Kling Apr 25 '16 at 17:36

2 Answers2

1

Yep, so I didn't realized that I hadn't added to my .babelrc file the stage-0 preset that includes async/await.

The code works just fine.

EDIT: As RGraham says, stage-3

armellini13
  • 377
  • 1
  • 3
  • 11
  • `stage-0` is only for strawman proposals. It includes lots of unfinalized behavior and is *definitely not* ES6. If you wanted to do this via a Babel preset, it would be [stage-3](https://babeljs.io/docs/plugins/preset-stage-3/) – CodingIntrigue Apr 26 '16 at 07:19
0

async/await is part of ES7 proposal. SO you need to use babel/traceur to compile your code to ES6/ ES5

Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
  • 3
    ES7 is already finalized (meaning, no new features are going to be added). `async/await` comes in a future version, earliest next year. – Felix Kling Apr 25 '16 at 18:05