Skip to content

Enhancement: [no-floating-promises] Add option to ignore catches #11421

@rnveach

Description

@rnveach

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-floating-promises/

Description

I propose that the rule should allow users to configure an option to ignore catches in promises and allow them to still be flagged as "floating". While this lint documentation specifically talks about a promise without error handling, I think of a "floating" promise as one having no direct connection to the rest of your code such that there is no flow control of when it interacts (or reconnects in interaction) with the rest of the code. It is "floating" in a separate space then the rest of the execution.

Take this code for example where you may try to test a method which has a promise with a catch:

  myField: number = null;

  myMethod() {
    Promise.resolve(() => {
      this.myField = 2;
    })
      .catch((error: unknown) => console.log(error));
  }

  it('should create', () => {
    component.myMethod();
    expect(component.myField).toBe(2); // field is still null, so test fails
  });

Adding all promise related lints with the defaults pass on this code.

            "@typescript-eslint/no-deprecated": "error",
            "@typescript-eslint/require-await": "error",
            "@typescript-eslint/return-await": "error",
            "@typescript-eslint/no-misused-promises": "error",
            "@typescript-eslint/no-floating-promises": "error",
            "@typescript-eslint/use-unknown-in-catch-callback-variable": "error",
            "@typescript-eslint/prefer-promise-reject-errors": "error",
            "@typescript-eslint/promise-function-async": "error",

The test fails because the promise hasn't resolved yet and we can't tell when it will finish since it isn't returned or awaited on, which would fix this. If it wasn't for the catch, no-floating-promises would flag it as a violation. So the catch doesn't help it interconnect with the rest of the execution. This is the type of code I want to violate on with proposed new option.

My recommendation for a option name would be ignoreCatches and it would ignore .catch(...) and .then() with two arguments. It would be a boolean, and the default could be false to maintain backwards compatibility.

In my project, I want to be able to force chain (sequential/parallel) all promises, and connect them together to ensure we have a strict flow of execution of the promises. This would require us to always return the promises up the chain if they ever exist. This is the reason I have created this issue and requesting this option.

Fail

myMethod() {
    Promise.resolve(() => { // violation, Promises must be awaited or returned
      this.myField = 2;
    })
      .catch((error: unknown) => console.log(error));
  }

Pass

async myMethod() { // subsequent change from other lints
    return Promise.resolve(() => { // resolved
      this.myField = 2;
    })
      .catch((error: unknown) => console.log(error));
  }

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy