npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-no-floating-promise

v2.0.0

Published

Detects missing await on async function calls

Downloads

196,221

Readme

eslint-plugin-no-floating-promise

Detects missing await on async function calls

STOP: Are you a Flow or a Typescript user? Prefer these:

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-no-floating-promise:

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

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

Usage

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

{
    "plugins": [
        "no-floating-promise"
    ]
}

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

{
    "rules": {
        "no-floating-promise/no-floating-promise": 2
    }
}

If you're using flat config (eslint.config.js), which is default configuration format for eslint since v.9.0.0:

const noFloatingPromise = require("eslint-plugin-no-floating-promise");

module.exports = [
  {
    plugins: {
      "no-floating-promise": noFloatingPromise,
    },
    rules: {
      "no-floating-promise/no-floating-promise": 2
    }
  }
];

Rule definition

The --fix option on the command line automatically fixes problems reported by this rule.

Promises that are never awaited can cause unexpected behavior because they may be scheduled to execute at an unexpected time.

It's easy to accidentally make this mistake. For example, suppose we have the code

function writeToDb() {
  // synchronously write to DB
}
writeToDb();

but the code gets refactored so that writing to the database is asynchronous.

async function writeToDb() {
  // asynchronously write to DB
}
writeToDb(); // <- note we have no await here but probably the user intended to await on this!

Rule Details

This rule will fire for any call to an async function that both

  • not used (not assigned to a variable, not the return type of a function, etc.)
  • not awaited on

Examples of incorrect code for this rule:

/*eslint no-floating-promise: "error"*/

async function foo() {}
foo();

(async () => 5)();

// note: function is not async but a Promise return type is specified
function foo(): Promise<void> { return Promise.resolve(); };
foo();

Examples of correct code for this rule:

/*eslint no-floating-promise: "error"*/

async function foo() {}
await foo();

await (async () => 5)();

// note: function is not async but a Promise return type is specified
function foo(): Promise<void> { return Promise.resolve(); };
await foo();

// note: promise is not awaited, but it is chained with a 'then'
async function foo() {}
foo().then(() => {});

You may catch additional errors by combining this rule with no-unused-expression.

For example the following will not be considered an error by this plugin, but no-unused-expression will complain that fooResult is never used.

async function foo() {}
const fooResult = foo();

When Not To Use It

If you often make use of asynchronous functions were you explicitly do not want to await on them.

Notable design decisions

Promises returned by non-async functions

It's possible for a function to return a promise and not be async (as seen in test cases for this rule). My rule can leverage type annotations to detect these cases but if no type annotation is present, then no error is reported. We could modify this rule to traverse into the AST to see if the return is a promise or not but this sounds more expensive and would still not catch all cases so I didn't include this logic.

Additionally, TypeAnnotations are only generated for explicit Flow types so this rule can't take advantage of Flow inference. You could argue this rule would be more effective if implemented directly in Flow but this rule can still catch cases for vanilla Javascript so it didn't feel right to make Flow a dependency for this kind of linting.

Scope of the rule

Currently using then or catch silences this rule

async function foo() {}
foo().then(() => {});

You could argue this is desired since it avoids false-positive where developers explicitly do not want to await. I'm open to feedback about whether or not people want this behavior.

Missing await in non-async context

Right now my rule reports an error in the following case

async function foo() {}
function bar() {
    foo();
}

However, adding an await here is not possible because bar is not an async function. In fact, my auto-fix rule would add an await in front of foo which would result in a compiler error.

However, likely this actually is an error and the user actually should make bar an async function. However, I am open to disabling this rule in this context if that's what people want.

Limitations

  1. It cannot make use of Flow inference (you need to explicitly specify types)
  2. It does not work across file boundaries (if import a function from a different file, you don't have any information)
  3. It doesn't work on member function calls (foo.bar() will not detect an error even if bar is an async function)