-
Notifications
You must be signed in to change notification settings - Fork 13
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
Proposal to disable no-return-await #78
Comments
I totally agree, and very happy for this explanations, feel free to make aPR |
Thanks for the detailed explanations! I do use the
pattern myself. About:
Would it be better to catch those exceptions at the call level of myFunc rather than inside myFunc? It ca be applied everywhere I guess but this may be what you want to do.
Do you mean that you would prefer to enforce the res = await.. pattern rather than removing those rules? One of the goal of no-return-await I believe is to force people to really understand what's going on by doing return await. Again, I have no strong opinions, you are the biggest await users here. |
The annoying thing that disabling this rule would fix is that if you return await, there can only be promise rejections, without it the function can throw an error and reject. Allowing return await prevents that, so I'm in favour of removing the rule. |
Can you precise what you mean |
This pattern is definitely ok, and I would start using it more if we keep the rule. async function myFunc() {
return await anotherFunc();
} I don't feel like we're losing any information here, while we do lose the function myFunc() {
return anotherFunc();
} This pattern is the one
My real-world code where this comes from is a wrapper which is supposed to send an error to sentry but not stop the execution if this fails.
I believe this is what @Haroenv meant: let i = 0;
async function myAsyncFunction() {
throw new Error('Error from async');
}
function myNonAsyncCallingAsyncFunction() {
// Throws on its first call
if (i++ === 0) throw new Error('Error from non async');
return myAsyncFunction();
}
for (let j = 0; j < 2; ++j) {
try {
// Notice how it is not awaited
myNonAsyncCallingAsyncFunction();
} catch (e) {
console.log(`Catch ${j} : `, e.message);
}
}
process.on('unhandledRejection', e => console.log('Unhandled:', e.message)); Output:
Basically, this If we add an
What I would like even more would be the ability to do the opposite : prevent Unfortunately, this is not possible, since a linter cannot know whether or not |
I have two issues with
no-return-await
.When there are multiple
awaits
no-return-await
tells you to changeinto
This is OK, until you start doing a
try...catch
here:Here if you add
await
after thereturn
again,eslint
will not complain anymore.But you've most of the time already removed it, and it doesn't jump to my eyes that
anotherFunc2
errors won't be catched.When there's only one await
For me, things are even worse with only one
await
.no-return-await
tells you to changeinto
which
require-await
tells you to change to:Suddenly, you have the same issue than the previous one, plus this function isn't marked
async
anymore, but you still need toawait
it if you call it.Now if you add a
try...catch
but don't know ifanotherFunc
isasync
, it's now even worse, because you have no indication whatsoever that there'sasync
stuff going on there:An alternative solution
There's another pattern where
eslint
won't complain:Now that I know about it, I'm thinking about using it everywhere I have such
returns
of non-explicit promises.Drawback
I assume that:
will actually create a wrapper promise on top of the other one, which could be concerning performance-wise.
I also believe that this is a case that should be easily optimizable by the compiler though.
Finally, I believe that the better clarity outweighs clearly the creation of a useless
Promise
instance.cc @vvo @Haroenv who I pinged on Slack :)
The text was updated successfully, but these errors were encountered: