59

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?

7
  • if you are using this a lot, build yourself a template that throws an error until all of the required stuff is provoided
    – Bindrid
    Jan 16, 2017 at 22:05
  • 6
    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 cases
    – Bojangles
    Jan 16, 2017 at 22:10
  • 1
    @Bojangles 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.
    – Leo Jiang
    Jan 16, 2017 at 22:11
  • 4
    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 keywords Jan 16, 2017 at 22:15
  • @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.
    – Leo Jiang
    Jan 16, 2017 at 22:23

3 Answers 3

42

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)

0
20

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

3
8
+25

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

Consider integrating the following:

2
  • 24
    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.
    – Bergi
    Aug 1, 2017 at 21:45
  • 7
    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/9787
    – davnicwil
    May 31, 2018 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.