Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I haveread the FAQ and my problem is not listed.
- There is a similar issue ([no-floating-promises] Ability to ignore certain types from producing errors #2640) regarding ignoring certain Promise-like types, but it was closed. This proposal operates on a much more specific level: indicating that a particular async function will never raise an error
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:
Additional Info
Versions
package | version |
---|---|
@typescript-eslint/eslint-plugin | 5.16.0 |
@typescript-eslint/parser | 5.16.0 |
TypeScript | 4.6.2 |
ESLint | 8.10.0 |
node | 16.12.0 |