1.2.0 • Published 1 year ago

eslint-plugin-no-needless-sync v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

eslint-plugin-no-needless-sync

A plugin to detect needlessly synchronised async function calls.

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install eslint-plugin-no-needless-sync:

npm install eslint-plugin-no-needless-sync --save-dev

Usage

Add no-needless-sync to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["no-needless-sync"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "no-needless-sync/needless-await": 2
  }
}

Examples

Bad

const a = await getSomething();
const b = await getOther();

Good

const [a, b] = await Promise.all([getSomething(), getOther()]);
// Note here b depends on the output of getSomething, so that's fine
const a = await getSomething();
const b = await getOther(a);

Bad

// Even though getSomething and getOther are parallelised,
// getFinal can also be included in the Promise.all call
const [a, b] = await Promise.all([getSomething(), getOther()]);
const c = await getFinal();

Good

const [a, b, c] = await Promise.all([getSomething(), getOther(), getFinal()]);

Support for more complex code

The rule can handle cases such as array and object assignments, dependencies inherited through the test clause of an if-statement, try-catch blocks.

Bad

const a = await getSomething();
if (unrelatedCondition) {
  await postSomething();
}

Good

const {
  data: { shouldPost },
} = await getSomething();
if (shouldPost) {
  await postSomething();
}

You can see explicitly supported cases in the integration spec.

Other useful additions

Please refer to the following:

When not to use this rule

When your code has implicit dependencies, e.g. you depend on errors being thrown in order to interrupt the control flow of your application, it is recommended to exclude the rule via a standard ESlint disable comment.