How to warn when you forget to `await` an async function in Javascript?
Asked Answered
G

3

64

I'm using Babel and Webpack. If I forget to await an async function, it can often go unnoticed. Once in a while, if I forgot the await, an error occurs in the async function and I get an Unhandled promise rejection. Then, I realize that I forgot the await.

Is there a way to get a warning when I forget to add an await?

Goodfornothing answered 16/1, 2017 at 21:59 Comment(7)
if you are using this a lot, build yourself a template that throws an error until all of the required stuff is provoidedUnderline
If you're using ESLint, might the require-await lint be enough for your needs? It won't catch imported async functions defined elsewhere but it will catch some casesDunne
@Dunne It will probably help a bit, but it definitely won't catch all the errors. I would like a way to require all async function calls to have an await before them.Goodfornothing
require all async function calls to have an await before them - I wouldn't recommend you require it, warning would be sufficient, because it is perfectly valid not to require to await an async function - in fact, at least one place in your code would have to call a function tagged async without using await, because of the relationship between await/async keywordsSamsun
@JaromandaX You're absolutely right. Also, awaiting an array of async functions would likely have async function calls without await directly in front of it (if we build the array dynamically). I guess what I'm looking for is a way to require await by default, but we can suppress the error explicitly. This way, it ensures that async functions calls without await were intentional.Goodfornothing
++ for ESLint, I'd say this is something that should be the task of your IDE.Seventeen
@LeoJiang: Not only the case you describe. The first async function that gets called cannot be awaited because await is only valid inside async. So the first async function that gets called returns a promise that you must call .then() on. Other async functions that that async function then call can be awaited. That's why JaromandaX says there MUST be at least one place in your code without awaitJudges
B
23

I think OP is looking for something like no-floating-promises from tslint see: https://palantir.github.io/tslint/rules/no-floating-promises/

Bolzano answered 22/9, 2017 at 23:26 Comment(3)
there is also an eslint variant: github.com/typescript-eslint/typescript-eslint/blob/master/…Reformatory
They seem to have added it Jun 10, 2019. Two years after this answer was posted :)Bolzano
Neither of these going to work if we don't use TypeScript right? Is there a comparable rule for JavaScript projects?Indrawn
B
49

The no-floating-promises ESLint rule requires you to explicitly handle any promises (e.g. with await or void).

Here's a minimal .eslintrc for use with TypeScript. Note that parserOptions is required for this rule:

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": { "project": "./tsconfig.json" },
  "plugins": ["@typescript-eslint"],
  "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
  "rules": {
    "@typescript-eslint/no-floating-promises": ["error"]
  }
}

(ESLint is recommended since TSLint has been deprecated.)

(Credit to @zaboco's comment)

Bender answered 16/8, 2020 at 13:59 Comment(0)
B
23

I think OP is looking for something like no-floating-promises from tslint see: https://palantir.github.io/tslint/rules/no-floating-promises/

Bolzano answered 22/9, 2017 at 23:26 Comment(3)
there is also an eslint variant: github.com/typescript-eslint/typescript-eslint/blob/master/…Reformatory
They seem to have added it Jun 10, 2019. Two years after this answer was posted :)Bolzano
Neither of these going to work if we don't use TypeScript right? Is there a comparable rule for JavaScript projects?Indrawn
S
9

Setup better eslint integration with webpack, repo, and code editor.
Here is the applicable rule, require await.

Consider integrating the following:

Symmetrize answered 23/1, 2017 at 20:31 Comment(2)
require-await doesn't really help. If the OP forgets to await a promise, he surely does also forget to make the function that should wait async.Medievalism
While this is better than nothing, it also doesn't catch the case where you have an async function with multiple awaits and you forget just a single one. It surprised me that eslint doesn't support a simple always await async functions rule but it turns out that this is quite complex to implement - see this thread github.com/eslint/eslint/issues/9787Exorcism

© 2022 - 2024 — McMap. All rights reserved.