eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Add `prefer-await` rule
Part of #652
The original plan is to add fix for many cases, but it's hard.
The reason I start to implement this rule is that promise/eslint-plugin-promise can't check .then in await expression.
I started to add fix for simple .then() only, but this rule already get huge.
I think we should merge this rule first, and try to fix more cases in separate PRs.
In this PR, there is no autofix for any case, it just simply check .then/catch/finnaly and provide suggestion for one case, so it should be safe to enable for everyone.
Would it be possible to add an option to allow .catch? I find this pattern pretty convenient over the alternative
const isConnected = await ping().catch(() => false);
const username = await getLoggedUser().catch(() => {}); // not logged in
If anything I’d probably almost always prefer .catch over a try/catch block, especially when I’m assigning a value. The alternative is suboptimal:
let isConnected = false;
try {
isConnected = await ping()
} catch {}
I'm personally ok with an option for that.
@fisker What should we do about this one?
@fisker Is this something you think you'll ever get to or should I close?
Let's close for now.