ネットワーク コードに最も役立つ eslint ルール (async/await/promises)


主な注意点は、typescript を使用する必要があることです.これを rules.eslintrc.json セクションに入れます.

"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/require-await": "error",
"@typescript-eslint/no-floating-promises": "error"


また、tsconfig ファイルの場所を eslint に伝える必要があります.

"parserOptions": {
    "project": "tsconfig.json"
}


次に、非同期呼び出しを待機しない、次のようなコードを記述した場合:

async function f() {
    await fetch('a')
    doSomethingElse()
    fetch('b')
}


次に、eslint 拡張機能を使用している場合、vscode のテキストの上にポップアップする役立つエラー メッセージが表示されます.

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

async 関数に待機がない場合、または同期関数を待機している場合にも、エラーが発生します.