0

Is there a way to detect missing await statements in Web JavaScript project?

Since JavaScript is so dynamic I doubt there is an analytic tool, but maybe is it possible using Google Web developer tool...

By missing is following code:

async createObject() => { ... }
async updateObject(object) => { ... }

// Incorrect
async () => {
  const object = createObject();
  await updateObject(object);
}
// Correct
async () => {
  const object = await createObject();
  await updateObject(object);
}
Charles
  • 11,367
  • 10
  • 77
  • 114
  • What do you mean by "missing"? A unit test which runs the code would likely see if an await keyword should be added. – evolutionxbox Oct 16 '21 at 17:09
  • any sample code to refer? – xdeepakv Oct 16 '21 at 17:09
  • 7
    Use ESLint with `require-await`. see more https://eslint.org/docs/rules/require-await – Arvind Oct 16 '21 at 17:11
  • @xdeepakv: Imagine any function marked `async` that doesn't have a corresponding `await`. – Robert Harvey Oct 16 '21 at 17:13
  • I think @Arvind added lint already :D – xdeepakv Oct 16 '21 at 17:14
  • I will try with ESLint but I wonder how ESLint is able to cover all the cases... I thought something during runtime would be more accurate... – Charles Oct 16 '21 at 17:16
  • Use ESLint or an IDE like VSCode or Webstorm/PHPStorm. They will help you with hints, autocompletion and type checking. – Jordan Breton Oct 16 '21 at 17:17
  • But I am not in favour of this. Sometimes you can use async to make a simple function promise to match another function call – xdeepakv Oct 16 '21 at 17:18
  • If you want something that cover all cases, you must take a look at Typescript. With the need to compile your code for it to work, it will complain if you try to assign a Promise into another variable. – Jordan Breton Oct 16 '21 at 17:18
  • 1
    One of the first results when searching for _"detect missing await"_ is eslint with `require-await`... -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Oct 16 '21 at 17:19
  • The project is already created with JavaScript and we notice we have some race condition bugs due to missing await... – Charles Oct 16 '21 at 17:20
  • 1
    `require-await` is not what I'm looking for. Please look closely at the example. `require-await` make sure that each `async` function has at least one await statement. My point is to make sure a `async` function is always called using await. Sorry if my initial question was not clear enough... – Charles Oct 16 '21 at 17:35
  • I think viable solution would be to write custom ESLint rule. https://eslint.org/docs/developer-guide/working-with-rules – Arvind Oct 16 '21 at 17:39
  • 2
    Forcing _every_ async function to _always_ be called with the result being `await`ed can basically undo all the benefits of async. What should happen in `list.forEach(async(e) => saveToDB(e))` where `saveToDB` is an async function too? If you force every async call to be awaited `await`, congratulations, now you're right back to "what was the point of using `async` at all here, then?". Rather than forcing this through linting, write proper tests (using Jest or something) that catch problems that stem from missing `await` keywords, so you can fix bad code, and _only_ bad code. – Mike 'Pomax' Kamermans Oct 16 '21 at 17:42
  • This looks more like an application bug. All above statement are correct from language perspective. As mentioned by @Mike'Pomax'Kamermans fixing bugs and writing proper test cases will catch most of the problems. I think another potential solution would be to write a custom ESLint rule, which can check for these assignments. https://eslint.org/docs/developer-guide/working-with-rules – Arvind Oct 16 '21 at 17:50
  • @Arvind `require-await` does not find the missing `await` keyword in front of calls to async functions – Bergi Oct 16 '21 at 17:59

0 Answers0