-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Feature request - rule for await in async try catch return #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
With eslint/eslint#12246 in mind (V8 providing better stack traces for |
Yes i think that'd make more sense @ark120202. @bradzacher what are you thoughts on that change should I update the feature request? |
To clarify then, The idea would be to have a rule ( Seems like a good candidate for this plugin. declare function returnsPromise(): Promise<string>;
declare function handleException(e: Error): string;
// option: 'in-try-catch'
async function myFunc1(): Promise<string> {
try {
return returnsPromise(); // lint error - should await the promise so errors are handled by the surrounding try/catch
} catch (e) {}
return Promise.resolve('foo'); // no error
}
// option: 'never'
async function myFunc1(): Promise<string> {
try {
return await returnsPromise(); // lint error - should never return await
} catch (e) {}
return await Promise.resolve('foo'); // lint error
}
// option: 'always'
async function myFunc1(): Promise<string> {
try {
return returnsPromise(); // lint error - should always await promise
} catch(e) {}
return Promise.resolve('foo'); // lint error
} |
that's what i was thinking, yeah! @ark120202 is the snippet above what you were thinking for this? |
Yeah, that sounds good to me! |
I'm actually okay with that since the same could be said for the |
@bradzacher so in terms of next steps should i put together a pr that references this issue? I haven't worked on a codebase like this in the past but I'd be happy to give it a go |
Actually, in that case is there a need for |
hmm interesting thought. Performance critical applications i suppose, but even then this is a pretty narrow optimization. @bradzacher did you have a particular use case in mind or just for consistency purposes? |
consistency, warding off people asking for it. I can pretty easily see an argument that people don't want return await ever, and would rather a variable assignment that gets returned a la async function myFunc1(): Promise<string> {
try {
const res = await returnsPromise();
return res;
} catch (e) {}
const res = await Promise.resolve('foo');
return res;
} Not sure - it's probably the easiest case to support, so I figure there's little overhead to supporting it. |
so in terms of next steps for this, should I open a PR? |
Definitely - if you see value in this and want it sooner, rather than later! It's always a lot faster and better for us if users PR the features we want; either we maintainers can try to write PRs for the ~180 issues in the repo, or we can review PRs for those issues. The latter is more scalable and easier. |
sounds good ill get to work on this, this weekend! |
return-await is not a recommended rule for users as part of default typescript-eslint setup. re typescript-eslint#994
A
Rejected Promise
returned by anasync
function will not be caught by the function'stry-catch
block. This may surprise some users and lead to unexpected behavior:conversely,
await
-ed promises work in a more intuitive fashion:I understand why this is the case according to the
async-await
function spec, but it feels like confusing behavior and a problem thattypescript-eslint
could help avoid via the following rule:disallow
return
-ing aPromise
from inside anasync
functiontry-catch
block.Thus, the first case described above would be invalid according to this rule, but the second case would be valid. We could even, potentially, provide an auto-fix that would add the
await
statement to the promise.There's also the possibility for the broader rule of disallowing
return
-ing aPromise
from anywhere inside anasync
function, but I wanted to start with the narrowest case.Please let me know if there's any other clarifying information I can provide, thanks so much for your time!
The text was updated successfully, but these errors were encountered: