eslint Parsing error: Unexpected token function with async

后端 未结 4 2017
长情又很酷
长情又很酷 2021-02-04 00:09

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

eslint Parsing error: Unexpected token function with async

Here is my

相关标签:
4条回答
  • 2021-02-04 00:32

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

    {
      "env": {
        "node": true,
        "es6": true
      },
    
      "parserOptions": {
        "ecmaVersion": 8
      }
    }
    
    0 讨论(0)
  • 2021-02-04 00:44

    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.

    0 讨论(0)
  • 2021-02-04 00:45

    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
        });
    }
    
    0 讨论(0)
  • 2021-02-04 00:51

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

    "parserOptions": { "ecmaVersion": 8 }

    to

    "parserOptions": { "ecmaVersion": 2018 }

    0 讨论(0)
提交回复
热议问题