How to find unneeded async statements in typescript code?

159 Views Asked by At

In our app, we have problems with developers using async where it is not needed (the code is syncronous) and this is sometimes causing problems with code running in unexpected order or boolean statements returning true where they should not, because the caller might not use await e.g. on a simple getter function.

Example:

async function aFunctionThatIsJustSyncronous() {
    return false;
}

// would evaluate to true
if(aFunctionThatIsJustSyncronous()) {
}

I am searching for ways to detect this and remove superfluous async statements in the code. Are there any linters that can detect this, or do we just have to watch out? It has bitten us some times in the past...

1

There are 1 best solutions below

0
Paul Weber On

After looking around i found some lints that might help us resolve this problem.

@typescript-eslint/no-misused-promises

https://typescript-eslint.io/rules/no-misused-promises/

This rule prevents you from forgetting to await an async function in a place where it would be easy to miss.

@typescript-eslint/no-floating-promises

https://typescript-eslint.io/rules/no-floating-promises

This rule prevents floating Promises in your codebase. A floating Promise is a Promise that doesn't have any code to handle potential errors.

Found this here: https://maximorlov.com/linting-rules-for-asynchronous-code-in-javascript