Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 490
Freewalker Avatar asked Dec 17 '25 17:12

Freewalker


1 Answers

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;
}
like image 74
Freewalker Avatar answered Dec 20 '25 13:12

Freewalker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!