MatAtBread / fast-async

605 stars 21 forks source link

"await is a reserved word" #60

Closed IngwiePhoenix closed 5 years ago

IngwiePhoenix commented 5 years ago

I will get right to the point:

.babelrc:

{
  "presets": [
    ["@babel/env", {
      "targets": {
        "node": "current"
      }
    }]
  ],
  "plugins": [
    "module:fast-async",
    ["babel-plugin-module-resolver", {
      "alias": {
        "BIRD3": "./src"
      }
    }],
    ["@babel/plugin-proposal-class-properties", {
      "loose": true
    }],
    ["@babel/plugin-transform-react-jsx", {
      "pragma": "h",
      "pragmaFrag": "fragment",
      "throwIfNamespace": false
    }]
  ]
}

Error:

{ SyntaxError: /Users/Ingwie/Work/DragonsInn/BIRD3/test2.js: await is a reserved word (8:14)

   6 |   init() {
   7 |     try {
>  8 |       let a = await someFunction();
     |               ^
   9 |       let b = await someFunction();
  10 |       console.log("Math:" + (a+b))
  11 |     } catch(e) throw e;
    at _class.raise (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:4021:15)
    at _class.checkReservedWord (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:7146:12)
    at _class.parseIdentifierName (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:7111:12)
    at _class.parseIdentifier (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:7103:21)
    at _class.parseExprAtom (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:6296:25)
    at _class.parseExprAtom (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:3717:52)
    at _class.parseExprSubscripts (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:6006:21)
    at _class.parseMaybeUnary (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:5985:21)
    at _class.parseExprOps (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:5894:21)
    at _class.parseMaybeConditional (/usr/local/lib/node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:5866:21)
  pos: 94,
  loc: Position { line: 8, column: 14 },
  code: 'BABEL_PARSE_ERROR' }

Versions:

Ingwie@Ingwies-Macbook-Pro.local ~/W/D/BIRD3 $ babel --version
7.1.5 (@babel/core 7.1.5)
Ingwie@Ingwies-Macbook-Pro.local ~/W/D/BIRD3 $ cat package.json | grep babel
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.5",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/plugin-transform-object-super": "^7.0.0",
    "@babel/plugin-transform-react-jsx": "^7.0.0",
    "@babel/preset-env": "^7.1.5",
    "babel-loader": "^8.0.2",
    "babel-plugin-module-resolver": "^3.1.1",
Ingwie@Ingwies-Macbook-Pro.local ~/W/D/BIRD3 $ cat package.json | grep fast-async
    "fast-async": "^7",
Ingwie@Ingwies-Macbook-Pro.local ~/W/D/BIRD3 $ node --version
v11.1.0

What I am trying to do is, to test various transformer plugins on the CLI before adding them to my WebPack build. I wanted to test fast-async to see if it transforms async and await in the way that I want.

While I am at it: How do I tell the plugin to only include the NoDent runtime once for many files, so it doesn't blow up the whole build in the end?

Well, I tried removing various entries from the .babelrc and even tried without a .babelrc entirely, with the plugin just specified on the CLI, and get the same error each and every time. What am I doing wrong here? :)

Also, the code I am trying to transpile:

async function someFunction() {
  return 1;
}

class Foo {
  init() {
    try {
      let a = await someFunction();
      let b = await someFunction();
      console.log("Math:" + (a+b))
    } catch(e) throw e;
  }
}

new Foo().init();
mauricecruz commented 5 years ago

You need to prefix your class method with async if you want to use await.

class Foo {
  async init() {
    try {
      let a = await someFunction();
      let b = await someFunction();
      console.log("Math:" + (a+b))
    } catch(e) throw e;
  }
}
matAtWork commented 5 years ago

@mauricecruz is right. Babel strictly enforces await inside async only.

nodent allows you to use await outside of async, but this is a non-standard extension. See https://github.com/MatAtBread/nodent#differences-from-the-es7-specification for more information