How can I use async/await in a es6 javascript clas

2020-04-21 01:13发布

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)

2条回答
可以哭但决不认输i
2楼-- · 2020-04-21 02:01

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

查看更多
狗以群分
3楼-- · 2020-04-21 02:05

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

查看更多
登录 后发表回答