Tslint: Cannot get `no-floating-promises` to work

Created on 29 May 2018  路  7Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.10.0
  • __TypeScript version__: Version 2.8.3
  • __Running TSLint via__: CLI (node_modules\.bin\tslint --project tsconfig.json *.ts *\*.ts)

TypeScript code being linted

import * as fs from "fs";
import * as util from "util";
const stat = util.promisify(fs.stat);

export async function isDirectory(path: string): Promise<boolean> {
    try {
        if ((await stat(path)).isDirectory()) {
            return true;
        }
    } catch (reason) {
        /* it's okay */
    }
    return false;
}

(async (): Promise<void> => {
    if (isDirectory("123")) {
        console.log("well, hello 123!");
    }
})();

with tslint.json configuration:

{
    "extends": "tslint:recommended",
    "rules": {
        "max-line-length": {
            "options": [80]
        },
        "no-floating-promises": true,
        "no-unused-expression": true,
        "no-unused-variable": true
    }
}

Actual behavior

TSLint does not warn about the return value of isDirectory() (called in the 4th-last line in the code snippet) being a Promise that is never handled (missing await).

Expected behavior

I would hope that TSLint saves me from the many awaits I regularly omit by mistake.

Documentation Duplicate Not A Bug

Most helpful comment

Same problem here, bit me multiple times already...

I have a simple async function that executes 1 function. That function returns a Promise<void> and omitting the await shows no tslint error.

All 7 comments

Looks like TSLint is examining expressions inside of conditionals; seems like a bug that should get fixed!

Status: Accepting PRs

@JKillian Okay, I'll bite.

However, I need guidance. I would appreciate pointers where to start fixing this (and where to add a regression test), but first I need to get npm run compile to run, which fails for me (both on Windows and on Linux):

$ npm run compile

> [email protected] compile /home/virtualbox/3rdparty/tslint
> npm-run-all -p compile:core compile:test -s compile:scripts


> [email protected] compile:test /home/virtualbox/3rdparty/tslint
> tsc -p test


> [email protected] compile:core /home/virtualbox/3rdparty/tslint
> tsc -p src


> [email protected] compile:scripts /home/virtualbox/3rdparty/tslint
> tsc -p scripts

node_modules/json-stringify-pretty-compact/index.d.ts(7,12): error TS2300: Duplicate identifier 'stringify'.
scripts/custom-typings.d.ts(3,14): error TS2300: Duplicate identifier 'stringify'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] compile:scripts: `tsc -p scripts`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] compile:scripts script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/virtualbox/.npm/_logs/2018-06-28T10_22_50_693Z-debug.log
ERROR: "compile:scripts" exited with 2.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] compile: `npm-run-all -p compile:core compile:test -s compile:scripts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] compile script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/virtualbox/.npm/_logs/2018-06-28T10_22_50_812Z-debug.log

What gives?

What gives?

I worked around, of course, by commenting out the custom typings, but it would still be good to know whether TSLint's source code is considered to be in a healthy state ;-)

Also, could you clarify what you mean by

Looks like TSLint is examining expressions inside of conditionals

Where in the code did you see that?

Oh, and it would appear that my understanding of what no-floating-promises is incorrect :-( There is an explicit test in test/rules/no-floating-promises/promises/test.ts.lint to verify that it does not report an unawaited Promise in a conditional:

while (returnsPromiseFunction());

So now I need even more guidance. Is this a bug in no-floating-promises' design? What is the sense of letting while (returnsPromiseFunction()); go unreported?

I have the same problem. It just doesn't work.

Same problem here, bit me multiple times already...

I have a simple async function that executes 1 function. That function returns a Promise<void> and omitting the await shows no tslint error.

while (returnsPromiseFunction());

@dscho yes, this is intentional. That is technically not a "floating" promise, as the promise is being used (as silly as that sounds).

This issue is a duplicate of #3983 and this should be better documented per #4117.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajinder-yadav picture rajinder-yadav  路  3Comments

Ne-Ne picture Ne-Ne  路  3Comments

ghost picture ghost  路  3Comments

allbto picture allbto  路  3Comments

zewa666 picture zewa666  路  3Comments