Freewalker
Freewalker

Reputation: 7315

ESLint: How to find unnecessary "async" markers on functions?

I'd like to remove any async modifiers from functions that do not need it. Specifically, those that neither return a promise nor have any await calls should not be marked async.

I understand not everyone may want this, e.g. a legacy code base may have lots of these where the callers are not worth changing. However for a new code base, I want to remove async wherever it's unnecessary, since that makes the function cleaner to use, and easier to test and reason about (it indicates the function does not rely on any asynchonous behavior - though it may still have async side effects).

It's easy for some of these to slip through, especially in cases where the only await call in an async function gets factored out, but the dev doesn't realize the function no longer needs to be async.

Is there some way to check this with ESLint or otherwise? I came up short in a web search.

Upvotes: 14

Views: 3346

Answers (1)

Freewalker
Freewalker

Reputation: 7315

Here's the TS rule: @typescript-eslint/require-await

And the vanilla JS ESLint version: require-await

That should prevent any useless async markings.

It properly flags a simple example:

export async function a() {
  return 1;
}

Upvotes: 15

Related Questions