Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[no-floating-promises] Provide a configuration option or other method to mark a Promise-returning function as "safe" #4722

Closed
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on
@ArcanoxDragon

Description

@ArcanoxDragon

Repro

I have encountered some cases while cleaning up some old JS code for my company (and converting it to TS in the process) where I want to register a.then() action on a Promise that I know will never be rejected. I don't want to have to usevoid whatever().then(...) onevery single instance where I am dealing with one of these promises, as they may be used very frequently and having to remember to putvoid before every single usage of a non-rejecting promise is tedious (and ugly). I still want the warning to appear for promises thatcould be rejected, however, in case somebody forgets to handle an actual situation where a rejection might occur.

One specific example is in an AJAX helper method I created that will normalize any AJAX request (be it from one of our APIs or from an MVC backend), show an error message if it failed for some reason, or resolve a promise if it was successful. Here is some simplified pseudocode that should convey the general idea:

functionsendAjaxAsync<Result=string>(options):Promise<Result>{returnnewPromise<Result>(res=>{makeRequestAndNormalizeResponse(options).then(response=>{if(response.isSuccessful){res(response.result);}else{showErrorMessage(response.errorMessage);}});});}letmyButton=$("#myButton");letmyContainer=$("#myContainer");myButton.on("click",()=>sendAjaxAsync({url:"/foo/bar/baz"}).then(html=>myContainer.append(html)));

If the request succeeds, the HTML from the response will be appended tomyContainer. If it fails for some reason,sendAjaxAsync will show an appropriate error message to the user, and the continuation will not run. There will be no runtime errors or rejections at all.

Another use case might be something like a wrapper aroundsetTimeout where the returned Promise isabsolutely guaranteed to never reject ever, and it wouldn't even make sense to consider the possibility of a rejection:

// never rejectsfunctiondoSomething():Promise<void>{/* ... */}// never rejectsfunctionwaitFor(ms:number):Promise<void>{returnnewPromise(res=>setTimeout(res,ms));}letmyButton=$("#myButton");letmyMessage=$("#myMessage");myButton.on("click",()=>{doSomething().then(()=>myMessage.show()).then(()=>waitFor(2000)).then(()=>myMessage.hide());});

I'm sure there are plenty of other similar cases out there as well.

I am proposing a configuration rule such as the following to tell theno-floating-promises rule that thePromise returned by certain functions willNEVER be rejected so that a simple single-callback.then() is enough for it to be treated as "not floating":

{"rules": {"@typescript-eslint/no-floating-promises": ["warn", {"ignoredFunctions": ["doSomething","waitFor"            ]        }]    }}

There may be a more elegant and/or flexible way of specifying this option; the example above is just one possibility.

Expected Result

I am expecting that, with the above hypothetical configuration, the example code would not have any linter warnings whatsoever.

Actual Result

Becauseno-floating-promises currently assumes thateveryPromise might be rejected, and thus must have a rejection handler registered 100% of the time, the above example raises a linter warning:

image

Additional Info

Versions

packageversion
@typescript-eslint/eslint-plugin5.16.0
@typescript-eslint/parser5.16.0
TypeScript4.6.2
ESLint8.10.0
node16.12.0

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-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp