SyntaxError: missing ) after argument list, When using async

Why am I getting this error When I use 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”:

{ "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" },

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

Upgrade to NodeJS 7.6 or later. 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.

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).

Just add these two modules to your dependencies

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

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

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

And if you are not using it already, install babel-cli and run your application with babel-node command

sudo npm install -g babel-cli

babel-node app.js

If you’re seeing this error with a newer version of Node, it’s probably a syntax or some other error before the line Node is pointing out.

For instance, consider the snippet below.

router.get("/", function (req, res, next) {
    try {
        res.json(await mySvc.myFunc());
    } catch (err) {
        console.error(err.message);
        next(err);
    }
});

With node -v reporting v14.17.6, this gives:

myapp $ DEBUG=myapp:* npm start

> [email protected] start /home/me/myapp
> node ./bin/www

/home/me/myapp/routes/myroute.js:7
        res.json(await mySvc.myFunc());
                 ^^^^^

SyntaxError: missing ) after argument list

The error, of course, is on the first line of the snippet. Adding an async on that line, thus,

router.get("/", async function (req, res, next) {

fixes the issue.

Read More:   How to convert object containing Objects into array of objects


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts