3

I'm migrating several projects from TSLint to ESLint and I've found that ESLint has no rule to prevent promises from being handled correctly, or at least I haven't found it in the docs.

The no-floating-promises rule in TSLint forced the programmer to, either async/await, or then/catch all promises. Is there a way to get that same functionality in ESLint?

I am aware of this answer, but I think OP is referring to a rule with a different behavior: catch() all promises. Moreover, the answer to that question is the npm package eslint-plugin-package, which hasn't had a release in two years.

perepm
  • 940
  • 9
  • 22

1 Answers1

2

typescript-eslint plugin has a rule that forces all promises to be handled correctly.

With ESLint installed:

Install the typescript-eslint plugin:

npm i --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin

Add it to your current configuration of ESLint:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: [
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],
};

If you chose "enforce popular coding style" and chose "Standard" when installing ESLint, you probably have this plugin installed. Albeit, if you have doubts, check your ESLint config file.

And add the rule to your rules object:

// .eslintrc.js
/* the rest of the configuration */
rules: {
    "@typescript-eslint/no-floating-promises": "error",
    /* the rest of the rules */
}
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
perepm
  • 940
  • 9
  • 22