question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a comment to an async function before the first argument can result in a syntax error

See original GitHub issue

Bug Report

Current behavior If you place a comment before the first (and only) argument of an async function (but after the async keyword), and the function has only a single argument, and if you use the await keyword inside that function, the compiled javascript won’t run because the function isn’t marked as async.

You get the following error when you run the compiled code:

babel-async-bug-repro/actual.js:5
  return foo(await a);
             ^^^^^

SyntaxError: missing ) after argument list
    at Module._compile (internal/modules/cjs/loader.js:720:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
    at internal/main/run_main_module.js:17:11

Here’s a full reproduction

Input Code

const x = async (
  // some comment <- this comment is the problem
  a
) => {
  return foo(await a);
};

function foo(a) {
  return a;
}

Expected behavior The compiled code should run without error. Presumably by making sure the async keyword is always on the same line as the function declaration

Babel Configuration (.babelrc)

The default config will generate the error (as of the day I posted this), also the following config will generate the error (note the borwserlist config is necessary to generate the error, otherwise regeneratorRuntime is used instead of emitting the async/await keywords):

  • Filename: .babelrc
{
  "presets": ["@babel/preset-env"]
}
  • Filename: package.json
  ...
  "scripts": {
    "build": "babel input.js --out-file actual.js",
    "test": "node actual.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.10.4",
    "@babel/core": "^7.10.4",
    "@babel/preset-env": "^7.10.4"
  },
  "browserslist": [
    "node 12",
    "last 1 Chrome version"
  ]

Environment

  System:
    OS: macOS Mojave 10.14.6
  Binaries:
    Node: 12.6.0 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  npmPackages:
    @babel/cli: ^7.10.4 => 7.10.4 
    @babel/core: ^7.10.4 => 7.10.4 
    @babel/preset-env: ^7.10.4 => 7.10.4 

Possible Solution I assume that always wrapping the arguments of an async function in parenthesis (and keeping the async keyword on the same line as the opening parentheses) would fix the issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cwohlmancommented, Jul 14, 2020

Sure, I can take a stab at it.

0reactions
cwohlmancommented, Jul 14, 2020

Ok, took a first stab: https://github.com/babel/babel/pull/11836, it’s not complete as I wasn’t able to fix the tests that depend on the old behavior (which should probably be preserved when it won’t cause a bug).

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the syntax error in my javaScript code? - Stack Overflow
I have this javascript function that calls an async function and stores the result in an array. I get a syntax error on...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration declares an async function where the await keyword is permitted within the function body. The async and await ...
Read more >
Async/await - The Modern JavaScript Tutorial
Let's start with the async keyword. It can be placed before a function, like this: async function f() { return 1; }. The...
Read more >
Asynchronous programming: futures, async, await - Dart
To interact with these asynchronous results, you can use the async and await keywords. ... First, add the async keyword before the function...
Read more >
14 Linting Rules To Help You Write Asynchronous Code in ...
First, if the async function throws, the error will be lost and won't be ... Therefore both functions add their result to totalPosts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found