New Rule: no async without await #3937
Comments
I think it's ok for warning the developer, but I want to leave this here: "An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function |
If you couldn't use async without await it was the compiler job to disallow this. In this case you will have an unhandled promise that will be resolves to |
this seems like a fair linter rule as @eranshabi go ahead with a PR! |
Only issue I see with this: async takesFnReturningPromise(closure: () => Promise<void>) {
// ...
await closure();
// ...
}
await takesFnReturningPromise(async () => {
// no await
}); The closure should still be async because |
I ran into the scenario @iluuu1994 pointed out. This rule would still be valuable but I wonder if there's a way to handle this exception, if so I think it would requiring typing to be enabled. If it can't be handled, there is a workaround to use this rule and get rid of |
The 'promise-function-async' rule requires that any function or method that returns a promise be marked as 'async', even if it is guaranteed that the function/method will always return a promise. e.g.
or
and the new rule 'no-async-without-await' requires any function or method without 'await' to be synchronous (non-async), even if there is no guarantee that the function/method will always return a promise. e.g.
Hopefully these two rules will eventually be combined into one useful rule in the future |
There's a valid reason to use |
isn't this what you guys are looking for? |
@a7madgamal |
@eranshabi do you have this rule anywhere? We'd really like to use it, and if it's not gonna be merged in here, we'd just run it from a fork. |
You can use it from here: https://github.com/wix-incubator/wix-tslint-custom-rules |
Hey folks - this issue is marked as A couple of notes on the discussion:
|
I already did a PR 6 months ago: #3945 |
👍 posted some comments on the PR @eranshabi |
FYI folks - this will be available in the next TSLint release. |
We wrote a rule which seems useful enough to go into TSLint.
The rule disallows async functions from not having
await
inside them, as from our experience theasync
modifier is almost always useless and the function could be a synchronous one. It will also cause TypeScript to compile the function into something much longer than a simple function.If this rule seems useful enough, we would like to PR it.
passing example:
failing example:
The text was updated successfully, but these errors were encountered: