Socket
Socket
Sign inDemoInstall

eslint-plugin-promise

Package Overview
Dependencies
0
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eslint-plugin-promise

Enforce best practices for JavaScript promises


Version published
Weekly downloads
3.7M
decreased by-17.47%
Maintainers
1
Created
Weekly downloads
 

Package description

What is eslint-plugin-promise?

The eslint-plugin-promise package is an ESLint plugin that enforces best practices and common idioms for dealing with promises in JavaScript. It provides a set of rules that catch common mistakes and enforce conventions when working with promises.

What are eslint-plugin-promise's main functionalities?

Avoiding creation of new promises outside of utility libraries

This rule ensures that the executor function of a new Promise has the parameters named 'resolve' and 'reject'.

/* eslint promise/param-names: 'error' */
new Promise((resolve, reject) => { /* executor function */ });

Ensuring that each time a then() is applied to a promise, a catch() is applied as well

This rule enforces the use of catch() on unhandled promises.

/* eslint promise/catch-or-return: 'error' */
promise.then(function(data) {
  // handle data
}).catch(function(error) {
  // handle error
});

Enforcing the return of a promise in certain contexts

This rule ensures that promise chains always return a value, preventing silent failures and unhandled rejections.

/* eslint promise/always-return: 'error' */
function foo() {
  return doSomething().then(function() {
    // do something else
    return 'result';
  });
}

Other packages similar to eslint-plugin-promise

Changelog

Source

1.0.0 - 1.0.7

  • Lots of basic feature updates and doc changes

Readme

Source

eslint-plugin-promise

Enforce best practices for JavaScript promises.

js-standard-style travis-ci npm version

Rule

always-catch

Ensure that each time a then() is applied to a promise, a catch() is applied as well.

Valid
myPromise.then(doSomething).catch(errors);
myPromise.then(doSomething).then(doSomethingElse).catch(errors);
Invalid
myPromise.then(doSomething);
myPromise.then(doSomething, catchErrors); // catch() may be a little better

always-return

Ensure that inside a then() you make sure to return a new promise or value. See http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html (rule #5) for more info on why that's a good idea.

Valid
myPromise.then((val) => val * 2));
myPromise.then(function(val) { return val * 2; });
myPromise.then(doSomething); // not sure
Invalid
myPromise.then(function(val) {});
myPromise.then(() => { doSomething(); });

param-names

Enforce standard parameter names for Promise constructors

Valid
new Promise(function (resolve) { ... })
new Promise(function (resolve, reject) { ... })
Invalid
// incorrect order:
new Promise(function (reject, resolve) { ... })
// non-standard parameter names:
new Promise(function (ok, fail) { ... })

Ensures that new Promise() is instantiated with the parameter names resolve, reject to avoid confusion with order such as reject, resolve. The Promise constructor uses the RevealingConstructor pattern. Using the same parameter names as the language specification makes code more uniform and easier to understand.

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-promise:

$ npm install eslint-plugin-promise --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-promise globally.

Usage

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

{
    "plugins": [
        "promise"
    ]
}

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

{
    "rules": {
        "promise/param-names": 2,
        "promise/always-return": 2,
        "promise/always-catch": 2
    }
}

Etc

Keywords

FAQs

Last updated on 22 Jan 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc