Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bigman73 opened this issue Nov 7, 2018 · 13 comments

Comments

@bigman73
Copy link

bigman73 commented Nov 7, 2018

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
Copy link

I can take a look at this.

@macklinu
Copy link
Contributor

macklinu commented Nov 8, 2018

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
Copy link
Author

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
Copy link

zone117x commented Feb 4, 2019

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

@xjamundx
Copy link
Contributor

xjamundx commented Feb 4, 2019

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
Copy link

zone117x commented Feb 5, 2019

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

@xjamundx
Copy link
Contributor

xjamundx commented Feb 5, 2019

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
Copy link

zone117x commented Feb 5, 2019

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

@vladimiry
Copy link

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

@bigman73
Copy link
Author

thanks, but I don't use typescript.

@sk-
Copy link

sk- commented Apr 12, 2020

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

@bigman73
Copy link
Author

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

@bigman73
Copy link
Author

bigman73 commented Jun 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants