eslint-community / eslint-plugin-promise

Enforce best practices for JavaScript promises
ISC License
938 stars 91 forks source link

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

Open bigman73 opened 5 years ago

bigman73 commented 5 years ago

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?

david-eighmey commented 5 years ago

I can take a look at this.

macklinu commented 5 years ago

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. 👍

bigman73 commented 5 years ago

Yes

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

zone117x commented 5 years ago

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

xjamundx commented 5 years ago

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

zone117x commented 5 years ago

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

xjamundx commented 5 years ago

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.

zone117x commented 5 years ago

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

vladimiry commented 4 years ago

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

bigman73 commented 4 years ago

thanks, but I don't use typescript.

sk- commented 4 years ago

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

bigman73 commented 4 years ago

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

bigman73 commented 4 years ago

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