sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-await`

Fermée

#652 ouverte le 31 mars 2020

 (28 commentaires) (25 réactions) (1 personne assignée)JavaScript (468 forks)user submission
help wantednew rule

Métriques du dépôt

Stars
 (5 022 étoiles)
Métriques de merge PR
 (Merge moyen 1j 16h) (399 PRs mergées en 30 j)

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

Guide contributeur