-
Notifications
You must be signed in to change notification settings - Fork 887
Conversation
Note: added configurable options in 6f932dd so additional class names of Promises can be added. |
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-floating-promises", | ||
description: "Promises returned by functions must be handled appropriately.", | ||
optionsDescription: Lint.Utils.dedent` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think populating the rationale
property here would be pretty useful. What does it mean to be handled "appropriately"? What happens when it's not handled?
@@ -0,0 +1,58 @@ | |||
class Promise { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add test cases for returnsPromiseFunction().then()
and calling static Promise functions
Following up on this PR: I haven't had time to get to it, but will in the next month. |
This should fix #892? |
@andy-hanson unfortunately no. Specifically ensuring async functions are awaited is a different matter than generally ensuring functions that return Promises have their results dealt with. The following code should be flagged by 892's solution, but not by this one: async function getSomething() {
return Promise.resolve(7);
}
async function runTest() {
const result = getSomething();
return result; // you may think this is a number, but it's actually `Promise<number>`
} |
Are you referring to syntax? Semantically "async functions" and "functions that return Promises" are the same, right? I don't think there's anything wrong with the code example you show here. Returning a Promise from an async function is OK. I interpreted the issue there as just not wanting any Update: Looks like the issue may be dealt with by TypeScript itself anyway, so no need for lint rules? microsoft/TypeScript#13376 (comment) |
@andy-hanson Actually it is recommended that you return the promise directly as appending await will create a promise that is resolved with the result of await expression. that means one unnecessary idempotent promise created. Arguably it should better be disallowed. |
I'm currently working on a rule for this. |
@nchen63 I believe your feedback has been resolved. Could you please take another look? I switched from a whitelist of allowed parent types to a blacklist, since there are much more places using a Promise generator would be allowed. |
can you rebase? |
It keeps a small blacklist of places functions that return Promises aren't allowed to be.
Rebased, but now the build is failing with |
try rebuild without cache |
Do you know if there's a way to permanently clear the cache in CircleCI? I have to rebuild without cache every time I push. |
Passed CircleCI, thanks for the tip. |
@JoshuaKGoldberg thanks! |
When turning on no-floating-promises, code like the following is getting flagged as a problem: something_that_produces_a_promise().then(() => fulfillPromise());
function fulfillPromise()
{
// do some stuff ...
resolve(whatever);
} That seems wrong to me - the Am I misunderstanding something, or is this a bug? Thanks! |
So your source looks something like this? return new Promise((resolve, reject) => {
something_that_produces_a_promise().then(() => fulfillPromise());
function fulfillPromise() {
// do some stuff ...
resolve(whatever);
}
}); Is there a reason you can't use return something_that_produces_a_promise()
.then(() => fulfillPromise())
.then(() => {
// do some stuff ...
return whatever;
}); I'm not convinced there's a frequently valid use case for using Also, this feels like a bug/feature request that might be better as a separate issue? |
I'd love to use The code does look like you described, though, and now I'm wondering whether I can just rewrite it to not use I imagine I could also just return I'll give that a shot and let you know how it turns out. Thanks! |
@JoshuaKGoldberg The following code is flagged:
How are you supposed to call the top level async function in an application? The following also fails:
What am I missing? Do you have to have a regular JavaScript file to call the top level function which returns a promise? Or just do a tslint ignore comment? |
Yup, either works. This should ideally only happen once per application. |
I think we should allow a promise as long as its errors are caught. See #2774. |
Promises must be stored or passed to a function.
The idea here is that floating (unhandled) Promises can cause unexpected behavior. For example, in tests, they might resolve at unexpected times (like after another test following their parent test has started).
Fixes microsoft/tslint-microsoft-contrib#174 (PRing here because I'm not sure they support the type checker).