Enforce best practices for JavaScript promises
npm install eslint-plugin-promiseEnforce best practices for JavaScript promises.



- Installation
- Usage
- Rules
- Maintainers
- License
You'll first need to install ESLint:
``sh`
npm install eslint --save-dev
Next, install eslint-plugin-promise:
`sh`
npm install eslint-plugin-promise --save-dev
Note: If you installed ESLint globally (using the -g flag) then you musteslint-plugin-promise
also install globally.
Add promise to the plugins section of your .eslintrc.json configurationeslint-plugin-
file. You can omit the prefix:
`json`
{
"plugins": ["promise"]
}
Then configure the rules you want to use under the rules section.
`json`
{
"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"
}
}
or start with the recommended rule set:
- eslint.config.js:
`js`
import pluginPromise from 'eslint-plugin-promise'
export default [
// ...
pluginPromise.configs['flat/recommended'],
]
- .eslintrc.*:
`json`
{
"extends": ["plugin:promise/recommended"]
}
💼 Configurations enabled in.\
⚠️ Configurations set to warn in.\
🚫 Configurations disabled in.\
✅ Set in the flat/recommended configuration.\recommended
✅ Set in the configuration.\--fix
🔧 Automatically fixable by the CLI option.
| Name | Description | 💼 | ⚠️ | 🚫 | 🔧 |
| :------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- |
| always-return | Require returning inside each then() to create readable and reusable Promise chains. | ✅ | | | |new
| avoid-new | Disallow creating promises outside of utility libs (use [util.promisify][] instead). | | | ✅ | |catch()
| catch-or-return | Enforce the use of on un-returned promises. | ✅ | | | |cb()
| no-callback-in-promise | Disallow calling inside of a then() (use [util.callbackify][] instead). | | ✅ | | |Promise
| no-multiple-resolved | Disallow creating new promises with paths that resolve multiple times. | | | | |
| no-native | Require creating a constructor before using it in an ES5 environment. | | | ✅ | |then()
| no-nesting | Disallow nested or catch() statements. | | ✅ | | |new
| no-new-statics | Disallow calling on a Promise static method. | ✅ | | | 🔧 |finally()
| no-promise-in-callback | Disallow using promises inside of callbacks. | | ✅ | | |
| no-return-in-finally | Disallow return statements in . | | ✅ | | |Promise.resolve
| no-return-wrap | Disallow wrapping values in or Promise.reject when not needed. | ✅ | | | |async
| param-names | Enforce consistent param names and ordering when creating new promises. | ✅ | | | |
| prefer-await-to-callbacks | Prefer /await to the callback pattern. | | | | |await
| prefer-await-to-then | Prefer to then()/catch()/finally() for reading Promise values. | | | | |catch
| prefer-catch | Prefer to then(a, b)/then(null, b)` for handling errors. | | | | 🔧 |
| spec-only | Disallow use of non-standard Promise static methods. | | | | |
| valid-params | Enforces the proper number of arguments are passed to Promise functions. | | ✅ | | |
- Jamund Ferguson - [@xjamundx][]
- Macklin Underdown - [@macklinu][]
- Aadit M Shah - [@aaditmshah][]
- (c) MMXV jden
- (c) 2016 Jamund Ferguson
[util.callbackify]:
https://nodejs.org/docs/latest/api/util.html#utilcallbackifyoriginal
[util.promisify]: https://nodejs.org/api/util.html#util_util_promisify_original
[@aaditmshah]: https://github.com/aaditmshah
[@macklinu]: https://github.com/macklinu
[@xjamundx]: https://github.com/xjamundx