A plugin to detect needlessly synchronised async function calls.
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
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
}
}
const a = await getSomething();
const b = await getOther();
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);
// 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();
const [a, b, c] = await Promise.all([getSomething(), getOther(), getFinal()]);
The rule can handle cases such as array and object assignments, dependencies inherited through the test clause of an if-statement, try-catch blocks.
const a = await getSomething();
if (unrelatedCondition) {
await postSomething();
}
const {
data: { shouldPost },
} = await getSomething();
if (shouldPost) {
await postSomething();
}
You can see explicitly supported cases in the integration spec.
Please refer to the following:
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.