eslint-plugin-promise icon indicating copy to clipboard operation
eslint-plugin-promise copied to clipboard

feature: warn when calling async functions without an await (no floating promises)

Open bigman73 opened this issue 7 years ago • 13 comments
trafficstars

Forgetting to put an await before calling an async function can have very bad outcomes and is quite hard to notice

TSLint has a rule named no-floating-promises -https://palantir.github.io/tslint/rules/no-floating-promises/

Can you implement a similar rule in this ESLint plugin?

bigman73 avatar Nov 07 '18 19:11 bigman73

I can take a look at this.

david-eighmey avatar Nov 07 '18 19:11 david-eighmey

Just to confirm, we are looking to warn about not awaiting an async function like this?

async function myAsyncFunction() {
  // ...
}

// ok, because of .then()
myAsyncFunction().then()

// ok, because of .catch()
myAsyncFunction().catch()

// ok, because awaited inside another async function
;(async () => {
  let value = await myAsyncFunction()
})

// not ok, because not awaited
;(async () => {
  let value = myAsyncFunction()
})

If you have other code examples that could help us catch all possible cases, please comment with more! Also, if I'm misunderstanding what syntax this rule would warn about, please let me know too - thanks. 👍

macklinu avatar Nov 08 '18 01:11 macklinu

Yes

Simply put, an async function is called without an await in front of it, and as a result it runs in the background

bigman73 avatar Nov 12 '18 22:11 bigman73

Surprised to find this still unsupported. This is a common cause of bug that are difficult to track down.

zone117x avatar Feb 04 '19 15:02 zone117x

You can't really do this in eslint, because it wouldn't know if a function is async unelss it was defined in that same file

xjamundx avatar Feb 04 '19 17:02 xjamundx

Do you mean eslint can't infer if a value is a Promise? I'm obviously to eslint's typing inference capabilities.

zone117x avatar Feb 05 '19 02:02 zone117x

Yeah it has no typing inference AFAIK. Think of it like this:

// car.js
export async function start() { /* ... */ }

// index.js
import { start } from './car'

async function main()  {
    start(); // eslint has no idea this should be awaited
}

Eslint could figure it out if they were defined in the same file though, but my strong feeling here is that a type system should be able to help with these problems and it's the way I'd recommend solving them.

xjamundx avatar Feb 05 '19 03:02 xjamundx

Oh I see. Another argument for using typescript. Thanks for the info.

zone117x avatar Feb 05 '19 12:02 zone117x

You could try @typescript-eslint/no-floating-promises rule if you go with TypeScript.

vladimiry avatar Mar 19 '20 22:03 vladimiry

thanks, but I don't use typescript.

bigman73 avatar Mar 19 '20 22:03 bigman73

@bigman73 have you looked at https://eslint.org/docs/rules/require-await

sk- avatar Apr 12 '20 15:04 sk-

Thanks, looks like a solution, I'll test it

bigman73 avatar Apr 12 '20 16:04 bigman73

UPDATE: It doesn't work. The rule warns when there is an async definition but no usage of await, but it doesn't warn against a function that is async and no await precedes the call

bigman73 avatar Jun 03 '20 15:06 bigman73