需要帮助使[create-react-app]使用Aync - Await(transform-async-to-generator)!

use*_*247 3 reactjs webpack babeljs create-react-app

我是[create-react-app]的新手,我想知道如何添加:["transform-async-to-generator"]到这个构建过程?在常规情况下,我会将它添加到.babelrc中,但不会在[create-react-app]中使用*.

*通过"看起来不工作" - 我看到以下错误.

Syntax error: ../web/src/App.js: Unexpected token, expected ( (17:13)

  15 |   }
  16 |
> 17 |   test = async () => {
     |              ^
  18 |     let x = await this.resolveAfter2Seconds();
  19 |     try{}
  20 |     catch(exception){
Run Code Online (Sandbox Code Playgroud)

是否有任何方法可以扩展[create-react-app],而无需修改包本身?

谢谢!

ser*_*gei 5

问题不在于异步功能.您应该以下一种方式重写代码:

// ...

test = async () => {
  let x = await this.resolveAfter2Seconds();
  // ...
}
Run Code Online (Sandbox Code Playgroud)

要么

// ...
async test(){
  let x = await this.resolveAfter2Seconds();
  // ...
}
Run Code Online (Sandbox Code Playgroud)