eslint Parsing error: Unexpected token function with async

亡梦爱人 提交于 2021-01-20 16:22: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');
};

回答1:


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

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

  "parserOptions": {
    "ecmaVersion": 8
  }
}



回答2:


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

"parserOptions": { "ecmaVersion": 8 }

to

"parserOptions": { "ecmaVersion": 2018 }




回答3:


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.




回答4:


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
    });
}


来源:https://stackoverflow.com/questions/50285821/eslint-parsing-error-unexpected-token-function-with-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!