DEV Community 👩‍💻👨‍💻

Cover image for Most useful eslint rules for networking code (async/await/promises)
Luke Harold Miles
Luke Harold Miles

Posted on

Most useful eslint rules for networking code (async/await/promises)

The main caveat is that you have to use typescript. Put this into the rules section of your .eslintrc.json:

"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/require-await": "error",
"@typescript-eslint/no-floating-promises": "error"
Enter fullscreen mode Exit fullscreen mode

You'll also need to tell eslint where your tsconfig file is:

"parserOptions": {
    "project": "tsconfig.json"
}
Enter fullscreen mode Exit fullscreen mode

Then if you write code like this, where you don't await an asynchronous call:

async function f() {
    await fetch('a')
    doSomethingElse()
    fetch('b')
}
Enter fullscreen mode Exit fullscreen mode

Then you'll get a helpful error message, which pops up over the text in vscode if you're using the eslint extension:

temp.ts
4:5   error  Promises must be handled appropriately or explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises
Enter fullscreen mode Exit fullscreen mode

You'll also get errors if an async function has no awaits or if you await a sync function.

Top comments (1)

Collapse
 
qpwo profile image
Luke Harold Miles

All the rules with more examples and explanation are in here: github.com/typescript-eslint/types...

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.