sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-await`

Chiusa

#652 aperta il 31 mar 2020

 (28 commenti) (25 reazioni) (1 assegnatario)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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

Guida contributor