sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-await`

Open

#652 建立於 2020年3月31日

在 GitHub 查看
 (28 留言) (25 反應) (1 負責人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

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) {
  // …
}

貢獻者指南