54

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?

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • 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 '17 at 22:05
  • 6
    If you're using ESLint, might the [`require-await`](http://eslint.org/docs/rules/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 '17 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 '17 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 – Jaromanda X Jan 16 '17 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 '17 at 22:23
  • `++` for ESLint, I'd say this is something that should be the task of your IDE. – John Weisz Jan 18 '17 at 22:18
  • @LeoJiang: Not only the case you describe. The first async function that gets called cannot be `await`ed 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 await – slebetman Jan 24 '17 at 22:11

3 Answers3

36

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)

fgblomqvist
  • 2,244
  • 2
  • 32
  • 44
Freewalker
  • 6,329
  • 4
  • 51
  • 70
19

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

ubershmekel
  • 11,864
  • 10
  • 72
  • 89
  • 12
    there is also an eslint variant: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-floating-promises.md – zaboco Jan 17 '20 at 09:49
  • 1
    They seem to have added it Jun 10, 2019. Two years after this answer was posted :) – ubershmekel Jan 17 '20 at 19:50
  • 2
    Neither of these going to work if we don't use TypeScript right? Is there a comparable rule for JavaScript projects? – barnesm999 Jan 11 '22 at 20:45
8

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

Consider integrating the following:

Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • 23
    `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 01 '17 at 21:45
  • 6
    While this is better than nothing, it also doesn't catch the case where you have an `async` function with multiple `await`s 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 https://github.com/eslint/eslint/issues/9787 – davnicwil May 31 '18 at 16:37