sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-await`

Open

#652 opened on Mar 31, 2020

View on GitHub
 (28 comments) (25 reactions) (1 assignee)JavaScript (5,022 stars) (468 forks)user submission
help wantednew rule

Description

The rule name may be named unicorn/prefer-await.

This only applies for ES6 code that have the await keyword available.

Prefer await over Promise#then(), Promise#catch() and Promise#finally()

Prefer using await operator over Promise#then(), Promise#catch() and Promise#finally(), which is easier to write and to read afterwards.

This rule is fixable.

Fail

await doSomething().then(() => {
  // …
});
doSomething().then(() => {
  // …
});
await doSomething().catch((err) => {
  // …
});
doSomething().catch((err) => {
  // …
});

Pass

 await doSomething();
try {
  await doSomething();
} catch (err) {
  // …
}

Contributor guide