Babel-eslint: error with await "Parsing error: Unexpected token"

Created on 8 Jun 2016  路  15Comments  路  Source: babel/babel-eslint

I am using a async/await library (https://github.com/yortus/asyncawait) to call Promises as if they are running synchronously. here is example:
it('Should update user info', async(function() {
let configInfo = await(userConfig.getConfig())
...
await(funds.waitForPaymentsMethodsScreen())
await(funds.navigateToAddCreditDebitCardSync())
....
}))
This code execute fine, but doesn't pass the eslint test. Running grunt eslint, I get following error.
"Parsing error: Unexpected token", for the parenthesis after await.

Is there way to resolve this error?

feedback invalid

Most helpful comment

@ThaJay await alone is invalid syntax. await has to be inside an async function like:

var a = async function() {
  let authToken = await get('authToken');
}

All 15 comments

And you are using babel-eslint? if its configured properly it should work fine. There isn't enough information here without your config, and version

Is this the information you are looking for, I am wondering what rule/configuration I need to avoid this error.

babel-eslint => "version": "6.0.4",

"rules": {
"arrow-body-style": 0,
"block-spacing": [2, "always"],
"comma-dangle": [2, "never"],
"constructor-super": 2,
"eqeqeq": [2, "smart"],
"func-names": 0,
"key-spacing": [2, { beforeColon: false, afterColon: true, mode: "minimum" }],
"max-len": [2, 120, 4, { ignoreComments: true }],
"no-array-constructor": 2,
"no-catch-shadow": 2,
"no-class-assign": 2,
"no-dupe-class-members": 2,
"no-empty-pattern": 2,
"no-extra-boolean-cast": 2,
"no-multi-spaces": 0,
"no-multiple-empty-lines": [2, { max: 1, maxEOF: 1 }],
"no-nested-ternary": 0,
"no-param-reassign": [2, { props: false }],
"no-self-compare": 0,
"no-this-before-super": 2,
"no-unexpected-multiline": 2,
"no-unused-expressions": 0,
"no-unused-vars": [2, { vars: "local", args: "none" }],
"no-useless-call": 2,
"no-void": 2,
"object-shorthand": [2, "methods"],
"prefer-arrow-callback": 0,
"prefer-template": 0,
"space-unary-ops": 2,
"wrap-iife": [2, "inside"]
}

The rules aren't necessary - I mean the other parts like parser: "babel-eslint"

you mean grunt config
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
eslint: {
options: {
parser: 'babel-eslint'
},
target: ['test/_.js', 'lib/__/_.js']
}
});
require('load-grunt-tasks')(grunt);
};

I've got a similar problem. My code looks like this:

const app = {
  init() {
    return async dispatch => {
      // do stuff
    }
  }
}

I receive following error

Parsing error: Unexpected token =>

Removing the parens doesn't help:

Parsing error: Unexpected token dispatch

Hi @hzoo, any suggestion on this issue?

@dan-lee You likely haven't set parser: "babel-eslint"in your eslint config.

@akhileshk await is a reserved word in ES6 modules, which is what Babel expects all files to be. The easiest solution to your problem would be to use real async functions instead of that module.

I have the same problem in my project.
.eslintrc:

{
  "parser": "babel-eslint"
, "parserOptions": {"ecmaFeatures": {"jsx": true, "esversion":6}}
, "env": {"browser": true, "node": true}
, "plugins": [ "react" ]
, "extends": ["eslint:recommended", "plugin:react/recommended"]
, "rules": { "react/prop-types": [0] }
}

in my code:

let authToken = await get('authToken')

marks get as Unexpected token

edit: same result when I add "es6": true to env.

@ThaJay await alone is invalid syntax. await has to be inside an async function like:

var a = async function() {
  let authToken = await get('authToken');
}

Is there anything to be done in here? I think all questions have been solved, right?

This is a common issue, the best thing we can do is to provide a better error message (if that's possible)

Annoying but when await is the last unexpected token can we say like it's possible you need async and point to the function?

Hey! The below flags "await" as a unexpected token.

async(() => {
  await( new Promise() );
});

While the below, does not.

async () => {
  await( new Promise() );
}

I'm using the asyncawait node library, https://github.com/yortus/asyncawait.

@danez Thanks, I indeed got that later. Did not read the async / await docs properly before I posted. In the end I decided to stick with new Promise / .then() as it's almost the same pattern and we found promises to predictable.

@Deepblue129 you have to use await in an async function. The former isn't that syntax. You would be calling a function named async with a non-async arrow function.

For me adding "es7": true to env fixed the issue.

It was complaining with this error on a for await loop.

Was this page helpful?
0 / 5 - 0 ratings