eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Add `prefer-await` rule

Open fisker opened this issue 3 years ago • 2 comments

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.

fisker avatar May 21 '22 14:05 fisker

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 {}

fregante avatar Jul 03 '22 06:07 fregante

I'm personally ok with an option for that.

sindresorhus avatar Jul 03 '22 11:07 sindresorhus

@fisker What should we do about this one?

sindresorhus avatar Feb 01 '23 15:02 sindresorhus

@fisker Is this something you think you'll ever get to or should I close?

sindresorhus avatar Oct 29 '23 18:10 sindresorhus

Let's close for now.

fisker avatar Oct 29 '23 18:10 fisker