简体   繁体   English

eslint 解析错误:带有异步的意外令牌函数

[英]eslint Parsing error: Unexpected token function with async

I am getting the following error in async usage on ESLINT.我在 ESLINT 的异步使用中遇到以下错误。

eslint Parsing error: Unexpected token function with async eslint 解析错误:带有异步的意外令牌函数

Here is my eslintsrc这是我的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');
};

I was getting this error also, I added the following to my eslintrc:我也收到此错误,我在 eslintrc 中添加了以下内容:

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

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

In my case it got fixed when I just changed from:在我的情况下,当我刚从以下位置更改时它得到了修复:

"parserOptions": { "ecmaVersion": 8 } “解析器选项”:{“ecmaVersion”:8}

to

"parserOptions": { "ecmaVersion": 2018 } “解析器选项”:{“ecmaVersion”:2018}

It's an error regarding func-style .这是关于func-style的错误。 By default it uses type expression , and the correct way to represent functions using this as expression is:默认情况下,它使用类型expression ,使用 this 作为expression来表示函数的正确方法是:

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检查文档以获取更多示例, 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.只需从 eslint 中删除func-style

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 包括异步 Function Firebase Cloud Functions 内(eslint“解析错误:意外的令牌函数”) - Including Async Function Within Firebase Cloud Functions (eslint "Parsing error: Unexpected token function") "“解析错误:意外的令牌 => eslint”" - "Parsing error: Unexpected token => eslint" Eslint:解析错误:意外的令牌…(分隔符ES) - Eslint: Parsing error: Unexpected token … (seperator ES) ESLint:解析错误:意外的令牌 instanceOf - ESLint: Parsing error: Unexpected token instanceOf 解析错误:使用eslint时出现意外的令牌.. - Parsing error: Unexpected token .. when using eslint 带有最新节点版本的异步 Function 上的“解析错误:意外令牌功能” - "Parsing Error: unexpected token function" on Async Function with a recent Node version Vue.js + eslint。 错误解析错误:意外的令牌@ - Vue.js + eslint. Error Parsing error: Unexpected token @ NodeJs 类静态属性 eslint 解析错误:Unexpected token = - NodeJs class Static atribute eslint Parsing error: Unexpected token = 如何使用异步功能修复“意外令牌”错误 - How to fix 'Unexpected Token' error with async function Firebase 云 Function - 解析错误:意外令牌 => - Firebase Cloud Function - Parsing error: Unexpected token =>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM