-
Notifications
You must be signed in to change notification settings - Fork 887
[Rule Request] no-misused-promises #3983
Comments
@vaskevich Very much agreed with your use cases! However, same as #3804 - what about Edit: re-reading your comment there, would your |
I think the issue with using |
It sounds like this request is logically equivalent to the {
"strict-boolean-expressions": [true, {
"object-types": "^Promise$"
}]
}
Edit: Sorry, re-read your previous response - yes, the first case of an |
Hello, After a tour of all the issues related to Promises in if statement, I am not sure of what is implemented, what is not, what is planned, and what we should use. @JoshuaKGoldberg does this config work ? should I use a specific branch ? Which direction on this topic has been chosen ? It seems that "strict-boolean-expression" might be the best choice, but not clear Glad to help if needed :) Best |
@eltonio450 great question. I recommend enabling
There's no way to make |
Ok thanks. Correct me if I am wrong but with this config, it becomes impossible to check if an object is defined by using:
|
Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you really need the rule, custom rules are always an option and can be maintained outside this repo! |
@JoshuaKGoldberg sure :-) |
* Add rule 'no-promise-as-boolean' This commit fixes #3983 * Review: fix grammar in rule rationale Co-Authored-By: Josh Goldberg <[email protected]> * Review: add extra test cases for union types * Review: use options object for no-promise-ad-boolean rule * Move es6 promise tests to separate folder * Review: add tests for third party promises * Add support for union typed promises in rule * Add extra test that awaits the promise * Review: add example for new noPromiseAsBoolean rule * Review: recommend other related rule * Fix build by using tsutils * Fix tslint issues * Review: fix typo Co-Authored-By: Josh Goldberg <[email protected]>
This is related to #3804 and of course
no-floating-promises
. There are a number of pitfalls when working with promises and async code.First off:
What happens here is that
Array#forEach
doesn't support promises natively, and will simply fire them off and run onto theDone
line before the URLs are fetched.printUrl()
has a return type ofPromise<void>
, but non-void functions in TS are assignable to void function parameters. Generally that rule is fine, but it'd be nice to handle promises specially to avoid this issue. One place where this can be a problem is in unit tests, where usingforEach
with async handlers can end up not actually running the test code.Here's another scenario:
It would be great to be able to catch this scenario (see #3804), though one thing to be cautious about is to still allow handling working with types like
Promise<any> | undefined
when explicitly needed to check whether a value is there or not. This could be allowed usingBoolean(promiseValue)
, for instance.Thoughts on whether handling these issues can be baked into an existing rule or be worthwhile to stand on its own as part of core rules?
The text was updated successfully, but these errors were encountered: