如何在es6 javascript类中使用async/await? [英] How can I use async/await in a es6 javascript class?

查看:1107
本文介绍了如何在es6 javascript类中使用async/await?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图使我创建的类具有异步功能,但是webpack在编译该类时给了我一个错误.

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

我的代码将是这样的:

class MyClass {

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

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

webpack给我的错误是:

The error that webpack gives me is:

Module build failed: SyntaxError: Unexpected token

(异步后意外令牌指向"u")

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

推荐答案

是的,所以我没有意识到我没有将包括async/await的Stage-0预设添加到我的.babelrc文件中.

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

代码工作正常.

正如RGraham所说,第3阶段

As RGraham says, stage-3

这篇关于如何在es6 javascript类中使用async/await?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆