-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
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