46

I am getting the following error in async usage on ESLINT.

eslint Parsing error: Unexpected token function with async

Here is my eslintsrc

{
  "extends": "airbnb-base",
  "rules": {
    "no-console": "off",
    "func-style":"error",
    "import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false, "packageDir": "./"}]
},
"parserOptions": {
  "ecmaVersion":8
 }
}

UPDATE

Here is my async

const get = async function get(req, res) {
  const user = await service.get();
  console.log("From db",user.username);
  res.send('ok');
};
shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129
  • Can you please also share the use of `async` it's raising the error about? – Jonathan Lonowski May 11 '18 at 05:58
  • @JonathanLonowski updated my async code – shamon shamsudeen May 11 '18 at 06:11
  • Not seeing the same error for the snippet, at least with [ESLint's demo](https://eslint.org/demo/). – Possibly a typo? You mention the settings are saved in `eslintsrc`. There shouldn't be a 2nd `s` in the file name. – Jonathan Lonowski May 11 '18 at 06:19
  • Its a typo the filename is `eslintrc` but still i am getting the same error from eslint – shamon shamsudeen May 11 '18 at 06:29
  • Just in case this is happening to people using `async/await` for the first time. This error also appears if you use `await` inside a function that is not defined as `async`. In which case it's not an EsLint configuration issue, but rather a syntax error. – Mig82 Nov 30 '20 at 09:48

4 Answers4

138

I was getting this error also, I added the following to my eslintrc:

{
  "env": {
    "node": true,
    "es6": true
  },

  "parserOptions": {
    "ecmaVersion": 8
  }
}
Damo
  • 5,698
  • 3
  • 37
  • 55
6

In my case it got fixed when I just changed from:

"parserOptions": { "ecmaVersion": 8 }

to

"parserOptions": { "ecmaVersion": 2018 }

darmis
  • 2,879
  • 1
  • 19
  • 21
1

It's an error regarding func-style. By default it uses type expression, and the correct way to represent functions using this as expression is:

const get = async get(req, res) {
  const user = await service.get();
  console.log("From db",user.username);
  res.send('ok');
};

Check the docs for further examples, https://eslint.org/docs/rules/func-style

UPDATE: Forgot to see you have added error, what you were doing was right,

const get = async function get(req, res) {
  const user = await service.get();
  console.log("From db",user.username);
  res.send('ok');
};

Just remove func-style from eslint.

-6

If you are new in the project I recommend just to go back with promises :)

function openTabs(array) {
    return new Promise((resolve, reject) => {
        //... your code
    });
}
ValRob
  • 2,584
  • 7
  • 32
  • 40
  • Bad answer. You shouldn't change something like this because of ESLINT, especially when it won't cause an exception when running it, this is just asking for a more tedious approach. – Epic Speedy Dec 02 '20 at 17:06