Open bigman73 opened 5 years ago
I can take a look at this.
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. 👍
Yes
Simply put, an async function is called without an await in front of it, and as a result it runs in the background
Surprised to find this still unsupported. This is a common cause of bug that are difficult to track down.
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
Do you mean eslint can't infer if a value is a Promise? I'm obviously to eslint's typing inference capabilities.
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.
Oh I see. Another argument for using typescript. Thanks for the info.
You could try @typescript-eslint/no-floating-promises
rule if you go with TypeScript.
thanks, but I don't use typescript.
@bigman73 have you looked at https://eslint.org/docs/rules/require-await
Thanks, looks like a solution, I'll test it
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
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?