14

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

 2 years ago
source link: https://dev.to/qpwo/most-useful-eslint-rules-for-networking-code-asyncawaitpromises-1mg4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Most useful eslint rules for networking code (async/await/promises)

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK