javascript SyntaxError: missing ) 在参数列表之后,使用异步时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46306997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:34:49  来源:igfitidea点击:

SyntaxError: missing ) after argument list, When using async

javascriptnode.jsasync-awaittelegram-bot

提问by Sedric Heidarizarei

Why am I getting this error When I use async?

为什么我在使用时收到此错误async

My Code:

我的代码:

bot.onText(/\/start/, async  msg => {
  const opts = {
    parse_mode: 'Markdown' ,
    reply_markup: JSON.stringify({
      keyboard: StartKeyboard,
      resize_keyboard: true,
      one_time_keyboard: true
    })
  };
  await bot.sendMessage(msg.chat.id, 'Hi', opts);
});

Error:

错误:

bot.onText(/\/start/, async  msg => {
                      ^^^^^
SyntaxError: missing ) after argument list

I'm using node.js v6.11.0 with "dependencies":

我正在使用带有“依赖项”的 node.js v6.11.0:

{ "babel-polyfill": "^6.23.0",
  "cheerio": "^1.0.0-rc.2",
  "dotenv": "^4.0.0",
  "firebase": "^4.1.2",
  "firebase-admin": "^5.0.0",
  "node-telegram-bot-api": "^0.27.1",
  "request": "^2.81.0" },

回答by Soviut

Your version of NodeJS (6.11 LTS) is too old and does not support the async/awaitfeatures. The syntax error is a result of the Javascript interpreter not recognizing the asynctoken and getting confused about arguments.

您的 NodeJS (6.11 LTS) 版本太旧,不支持这些async/await功能。语法错误是由于 Javascript 解释器无法识别async令牌并且对参数感到困惑。

Upgrade to NodeJS 7.6 or later. https://www.infoq.com/news/2017/02/node-76-async-await

升级到 NodeJS 7.6 或更高版本。https://www.infoq.com/news/2017/02/node-76-async-await

In prior versions, the only way to perform asynchronous behaviour is to use promises.

在以前的版本中,执行异步行为的唯一方法是使用promises

回答by Thiago Loddi

If you don't want to/can't update your node version, try using babel presets. I had the same error using ES6 with jest (node v6.9.1).

如果您不想/无法更新您的节点版本,请尝试使用 babel 预设。我在使用 ES6 时遇到了同样的错误(节点 v6.9.1)。

Just add these two modules to your dependencies

只需将这两个模块添加到您的依赖项中

npm install --save babel-preset-es2015 babel-preset-stage-0

And add a file .babelrcto your root dir with the following code:

.babelrc使用以下代码将文件添加到您的根目录:

{ "presets": ["es2015", "stage-0"] }

And if you are not using it already, install babel-cliand run your application with babel-nodecommand

如果您还没有使用它,请babel-cli使用babel-node命令安装并运行您的应用程序

sudo npm install -g babel-cli

babel-node app.js