DEV Community

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

Posted on

6 1

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
qpwo profile image
Luke Miles

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay