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.

New Rule Proposal: Consistent async function naming

See original GitHub issue

Please describe what the rule should do:

This rule enforces that async functions are named with an Async suffix and also that any function named with an Async suffix be marked as async. It is a common practice for functions that return promises to be named with an Async suffix to indicate to the caller that they should handle a promise. (They either need to await the result, call .then, etc.) In fact, this is also the standard naming convention in .NET, which has a similar async/await pattern.

What category of rule is this? (place an “X” next to just one item)

[x] Enforces code style [ ] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

Incorrect versions:

async function doSomething() {
    // ...
}

function doSomethingElseAsync() {
    // ...
}

Correct versions:

async function doSomethingAsync() {
    // ...
}

async function doSomethingElseAsync() {
    // ...
}

Why should this rule be included in ESLint (instead of a plugin)?

The rule inforces consistency of a very common practice (from even before the async keyword existed). It is general, library agnostic, and shouldn’t conflict with other rules.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dtjohnsoncommented, May 18, 2017

Seems fair that you don’t want the core to be so opinionated. @not-an-aardvark’s suggestion and @platinumazure’s example were a great start. However, there are a number of cases to cover:

async function fooAsync() {
}

function foo() {
}

const barAsync = async function () {
};

const bar = function () {
};

const bazAsync = async () => {
};

const baz = () => {
};

class Foo {
    async fooAsync() {
    }

    foo() {
    }
}

const obj = {
    async fooAsync() {
    },

    foo() {
    },

    barAsync: async function () {
    },

    bar: function () {
    },

    bazAsync: async () => {
    },

    baz: () => {
    }
};

I expanded the rule a bit to cover all of these cases and it seems to catch all of these:

{
  "no-restricted-syntax": [
    "error",
    {
      "selector": "FunctionDeclaration[async=false][id.name=/Async$/]",
      "message": "Function ending in 'Async' must be declared async"
    },
    {
      "selector": "FunctionDeclaration[async=true][id.name!=/Async$/]",
      "message": "Async function name must end in 'Async'"
    },
    {
      "selector": "MethodDefinition[value.async=false][key.name=/Async$/]",
      "message": "Method ending in 'Async' must be declared async"
    },
    {
      "selector": "MethodDefinition[value.async=true][key.name!=/Async$/]",
      "message": "Async method name must end in 'Async'"
    },
    {
      "selector": "Property[value.type=/FunctionExpression$/][value.async=false][key.name=/Async$/]",
      "message": "Function ending in 'Async' must be declared async"
    },
    {
      "selector": "Property[value.type=/FunctionExpression$/][value.async=true][key.name!=/Async$/]",
      "message": "Async function name must end in 'Async'"
    },
    {
      "selector": "VariableDeclarator[init.type=/FunctionExpression$/][init.async=false][id.name=/Async$/]",
      "message": "Function ending in 'Async' must be declared async"
    },
    {
      "selector": "VariableDeclarator[init.type=/FunctionExpression$/][init.async=true][id.name!=/Async$/]",
      "message": "Async function name must end in 'Async'"
    }
  ],
}

If you don’t feel something like this should be in the core, then at least this issue serves as an example for other folks who want to follow this async pattern. Thanks!

1reaction
ilyavolodincommented, May 2, 2017

I agree with @not-an-aardvark I think this is pretty opinionated rule for the core, and should be part of a plugin (or as @platinumazure showed, should just be part of the configuration). Although I’m biased. I hate prefixes and postfixes for variable/function names.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do people add Async to method names in new code?
I do append "Async" to the name of all my async methods for two ... Consistency and sticking to group style is important...
Read more >
[Amendment] SE-0296: Allow overloads that differ only in async
Given a call, overload resolution prefers non- async functions within a synchronous context (because such contexts cannot contain a call to an  ......
Read more >
Understand promises before you start using async/await
I just call `then()` in either case. As such, promises force consistent asynchronicity (and avoid releasing zalgo). It's like saying, 'this is ...
Read more >
Naming conventions | Cloud APIs
Generally, method names with prepositions indicate that a new method is being used where a field should instead be added to an existing...
Read more >
Google JavaScript Style Guide
6 Naming: 6.1 Rules common to all identifiers: 6.2 Rules by identifier type ... 8 Policies: 8.1 Issues unspecified by Google Style: Be...
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