ESLint Plugin Promise is a comprehensive ESLint plugin that enforces best practices for JavaScript promises and async/await patterns. It provides 17 different rules to help developers avoid common pitfalls such as forgetting return statements, improper error handling, nested promises, callback mixing, and invalid promise usage patterns.
npm install eslint-plugin-promise --save-devESM (flat config):
import pluginPromise from 'eslint-plugin-promise';CommonJS (legacy config):
const pluginPromise = require('eslint-plugin-promise');import pluginPromise from 'eslint-plugin-promise';
export default [
// ... other configs
pluginPromise.configs['flat/recommended'],
];{
"plugins": ["promise"],
"extends": ["plugin:promise/recommended"]
}{
"plugins": ["promise"],
"rules": {
"promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"promise/no-native": "off",
"promise/no-nesting": "warn",
"promise/no-promise-in-callback": "warn",
"promise/no-callback-in-promise": "warn",
"promise/avoid-new": "warn",
"promise/no-new-statics": "error",
"promise/no-return-in-finally": "warn",
"promise/valid-params": "warn",
"promise/no-multiple-resolved": "error"
}
}ESLint Plugin Promise is built around several key components:
Core plugin configuration including recommended rule presets and legacy compatibility options.
const pluginPromise = {
rules: { [ruleName: string]: ESLintRule },
configs: {
recommended: ESLintConfig,
'flat/recommended': ESLintFlatConfig
},
rulesConfig: { [ruleName: string]: number } // Legacy
};Comprehensive set of 17 ESLint rules for enforcing promise best practices, covering error handling, return statements, nesting, callback integration, and parameter validation.
// Core recommended rules
const recommendedRules = {
'promise/always-return': 'error',
'promise/no-return-wrap': 'error',
'promise/param-names': 'error',
'promise/catch-or-return': 'error',
'promise/no-native': 'off',
'promise/no-nesting': 'warn',
'promise/no-promise-in-callback': 'warn',
'promise/no-callback-in-promise': 'warn',
'promise/avoid-new': 'off',
'promise/no-new-statics': 'error',
'promise/no-return-in-finally': 'warn',
'promise/valid-params': 'warn'
};interface ESLintRule {
meta: {
type: 'problem' | 'suggestion' | 'layout';
docs: {
description: string;
url: string;
};
fixable?: 'code' | 'whitespace';
schema: any[];
messages: { [messageId: string]: string };
};
create(context: ESLintContext): ESLintVisitor;
}
interface ESLintConfig {
plugins: string[];
rules: { [ruleName: string]: string | number };
}
interface ESLintFlatConfig {
name: string;
plugins: { [pluginName: string]: ESLintPlugin };
rules: { [ruleName: string]: string | number };
}
interface ESLintContext {
report(descriptor: ReportDescriptor): void;
getSourceCode(): SourceCode;
// ... additional ESLint context methods
}
interface ReportDescriptor {
node: ESTreeNode;
messageId: string;
data?: { [key: string]: string };
fix?(fixer: RuleFixer): Fix;
}