Tslint: Suppressing no-floating-promises on specific function

Created on 19 Jul 2017  路  4Comments  路  Source: palantir/tslint

TypeScript code being linted

// tslint:disable-next-line:no-floating-promises
async function asyncFunction() { ... }

asyncFunction(); // no error for this function

Could it be possible?

Most helpful comment

Use the void keyword when calling the function to indicate that you are not interested in the returned value:

void asyncFunction();

All 4 comments

Use the void keyword when calling the function to indicate that you are not interested in the returned value:

void asyncFunction();

I never knew about void technique. 馃憤
However, it seems like it's not different with this.

// tslint:disable-next-line:no-floating-promises
asyncFunction();

You're right, disabling the rule for that line has the same effect.

There's a reason I suggested void. There's an open issue (and a PR https://github.com/Microsoft/TypeScript/pull/15195) for the typescript compiler to disallow floating promises in async functions. For example:

async function foo() {
    asyncFunction(); // will result in an error
}

To avoid this error, the proposed workaround is to add the void keyword https://github.com/Microsoft/TypeScript/issues/13376#issuecomment-273289748

async function foo() {
    void asyncFunction(); // no error
}

I got it. thank you for the information.
There will be new compiler options on async/await, and it'll be better to use them in the future.

Was this page helpful?
0 / 5 - 0 ratings