-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Rule proposal: prefer-await
#652
Comments
try...catch
over .catch()
unicorn/prefer-try-catch-over-catch-method
unicorn/prefer-try-catch-over-catch-method
prefer-try-catch-over-catch-method
How about Second thought, if we forbid |
Yeah right |
There is one rule called There is also proposal in eslint project, eslint/eslint#9649 and also a PR eslint/eslint#11962 😄 |
Sure but the repo looks a bit abandoned. There is also some issues with this rule 😅 like this one. Or that fact that it only catches
Looks like they abandoned this rule in to be integrated in core ESLint rule for performance control reasons 🤔 |
I'm 👍 on adding a |
I like this, remember I was looking for similar rule before. We need discuss details.
|
prefer-try-catch-over-catch-method
prefer-await
|
I tried a little bit to implement this rule, starting on fixing |
promise.then(onFulfilled, onRejected); Shouldn't fix to try {
onFulfilled(await promise);
} catch (error) {
onRejected(error);
}
So, the following one is right? let result;
try {
result = await promise;
} catch (error) {
onRejected(error);
}
onFulfilled(result); |
@fisker maybe the rule should not fix that kind of case (that requires a new variable).. and the rule should be partially fixable. Note that this is not an acceptable fix because we don't want to catch try {
onFulfilled(await promise); // No good, we don't want to catch `onFullfilled`
} catch (error) {
onRejected(error);
} |
I have lots of legacy codes need rewrite from my work, I want a fix so much. |
Yeah but what if a |
This exists in |
Reopening this, since |
Hi @fisker, you might be interested in https://github.com/codemodsquad/asyncify by @jedwards1211 - it was used to convert the whole codebase of https://github.com/sequelize/sequelize from Bluebird promises to async-await. |
Thanks, I'll take a look. |
It would probably be the most complicated autofix ever 🤣 |
Also I never implemented support for Flow or TS type annotations in my codemod, which would probably be a minimum requirement for an eslint autofix. Would probably take a lot more work to fully support them |
Thanks for you information, |
Hmmm, actually type annotations aren't a huge problem, I just fixed an issue with catch error type annotations, but the argument type annotations already get transferred by If you mean convert to an ESLint autofix, I'm sure it's possible, but the main obstacle is Plus, people might have preferences for what the autofix does. For example, not everyone would want to unroll oneliners like try {
return await foo
} catch (err) {
return null
} So I'm not sure at autofix would satisfy everyone. |
Another consideration: a lot of code has to get moved around in an autofix. I used |
Thanks a lot, I think maybe we can start adding auto-fix for some simple case, we don't have to handle all cases, as it will still be reported. |
Oh and @papb thanks for the shout-out! |
@jedwards1211 And thank you for all that hard work 😁 |
I personally prefer the 5 whole lines as I want developers to catch specific errors and also add comments about why the error needs to be caught. Catching an error is something that should be done only for specific scenarios that needs to be documented and well controlled. That's why we have the catch-error-name rule. try {
return await foo
} catch (err) {
if (err.name === "bar") {
// Error is swallowed because blabla.
return null;
}
throw err;
} In my opinion code like |
@yvele even if you do handling like that in a catch callback, I still prefer the catch callback sometimes, because try/catch is so ungainly to use. Especially if you have to hoist a variable declaration outside of the try block, it's pretty terrible (and gets even more cumbersome tamping down Flow errors on the hoisted variable, since it can't be |
With const result: Foo | null = await foo.catch((err: Error) => {
if (err.name === "bar") {
// Error is swallowed because blabla.
return null;
}
throw err;
}) |
I agree that let result: Foo;
try {
result = await foo;
} catch(err: unknown) {
if (err.name !== "bar") {
// Error is swallowed because blabla.
throw err;
}
} |
The rule name may be named
unicorn/prefer-await
.This only applies for ES6 code that have the
await
keyword available.Prefer
await
overPromise#then()
,Promise#catch()
andPromise#finally()
Prefer using
await
operator overPromise#then()
,Promise#catch()
andPromise#finally()
, which is easier to write and to read afterwards.This rule is fixable.Fail
Pass
The text was updated successfully, but these errors were encountered: