eslint-plugin-async-promise
A typescript-eslint plugin to catch Promise related errors
Installation
First, install the required packages for ESLint, TypeScript, TypeScript ESLint and the TypeScript ESLint Parser
npm install @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint typescript --save-dev
Next, install eslint-plugin-async-promise
:
$ npm install eslint-plugin-async-promise --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must
also install eslint-plugin-async-promise
globally.
Usage
Add async-promise
to the plugins section of your .eslintrc.json
configuration
file. You can omit the eslint-plugin-
prefix:
{
"plugins": ["async-promise"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"async-promise/async-no-await": "error",
"async-promise/unnecessary-async": "warn"
}
}
Rules
async-no-await
Disallow concurrent async function calls inside an async function.
While we do not always need to await
Promise (such as firing side-effects that do not block other calls), it is often a programmer error to forget adding await
for these async callsinside an async function.
Examples
❌ Incorrect
function a() {
return Promise.resolve();
}
async function b() {
a(); // a() should have been awaited
return Promise.resolve();
}
function a() {
return Promise.resolve();
}
async function b() {
a(); // a() should have been awaited since the return type is still Promise implicitly
return "Not returning a Promise";
}
✅ Correct
function a() {
return Promise.resolve();
}
async function b() {
await a(); // Even if b() is not awaited, a() will always run before the return
return Promise.resolve();
}
function a() {
return Promise.resolve();
};
function b() {
a(); // This is ok because the function is not marked as async, this can just be a side-effect
return Promise.resolve();
};,
function a() {
return Promise.resolve();
}
async function b() {
const result = await Promise.all([a()]); // Skip checks async calls in Promise.all, if it's awaited
return Promise.resolve();
}
unnecessary-async
Enforce that async
keyword is only added when:
- The function explicitly returns Promise
- There is at least 1
await
statement in the function
Examples
❌ Incorrect
function a() {
return Promise.resolve();
}
async function b() {
a();
}
async function b() {
return "Not returning a Promise explicitly";
}
✅ Correct
async function b() {
return Promise.resolve();
}
function a() {
return Promise.resolve();
};
async function b() {
await a();
};,
function a() {
return Promise.resolve();
};
async function b() {
return a();
};,
const a = {
b: () => Promise.resolve(),
};
async function c() {
// Example: MemberExpression : a.b()
const result = await a.b();
return result;
}