Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect missing await in JavaScript methods in VSCode

I'm searching for some eslint option, or some other way to detect missing the 'await' keyword before calling async methods inside a class. Consider the following code:

const externalService = require('./external.service');

class TestClass {

constructor() { }

async method1() {
    if (!await externalService.someMethod()) {
        await this.method2();
    }
}

async method2() {
    await externalService.someOtherMethod();
}

module.exports = TestClass;

There will be no warning if I will convert method1 to:

async method1() {
    if (!await externalService.someMethod()) {
        this.method2();
    }
}

I tried to do on the '.eslintrc' file:

"require-await": 1,
"no-return-await": 1,

But with no luck. Anyone have an idea if it is even possible? Thanks a lot!

like image 805
Or Assayag Avatar asked Feb 22 '26 05:02

Or Assayag


1 Answers

typescript-eslint has a rule for this: no-floating-promises

This rule forbids usage of Promise-like values in statements without handling their errors appropriately ... Valid ways of handling a Promise-valued statement include awaiting, returning, and either calling .then() with two arguments or .catch() with one argument.

As you probably figured out from the name, typescript-eslint is designed to add TypeScript support to eslint, but you can use it with JavaScript as well. I guess it's your call to decide whether it's overkill for this one rule, but here are the steps:

  1. Generate a tsconfig.json file

    npx tsc --init
    
  2. Install dependencies

    npm install --save-dev eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
    
  3. Modify your .eslintrc file

    Based on my testing it looks like you'll need these entries at a minimum:

    {
      "parser": "@typescript-eslint/parser",
      "parserOptions": { "project": "./tsconfig.json" },
      "plugins": ["@typescript-eslint"],
      "rules": {
        "@typescript-eslint/no-floating-promises": ["error"]
      }
    }
    

If there are places where you want to call an async function without using await, you can either:

  • Use the void operator as mentioned in the documentation for the rule, e.g.

    void someAsyncFunction();
    
  • Or just change error to warn in the .eslintrc configuration above

The documentation for setting up typescript-eslint is here for more info: https://typescript-eslint.io/docs/linting/linting

Next time you run eslint you should see the rule applied:

$ npm run lint
...
./services/jobService.js
  11:5  warning  Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises

Since you mentioned VS Code specifically, this also integrates great with the ESLint plugin:

vscode showing no-floating-promises rule

like image 66
bmaupin Avatar answered Feb 24 '26 17:02

bmaupin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!