nico.fyi
Published on

What is "async" must be await-ed

Authors
  • avatar
    Name
    Nico Prananta
    Twitter
    @2co_p

Several months back, I experienced a sudden crash in my pull request on the Monika project, which caught my team off guard during the PR review, as noted in this issue comment. I was surprised because I expected any errors to be intercepted by the try-catch blocks already in place. However, the error slipped through.

After extensive debugging, I realized the root cause: the error was originating from an async function that wasn't invoked with await. Here's an illustration:

const main = async () => {
  try {
    doSomething() // the bug
  } catch (error) {
    console.log('here?')
  }
}

const doSomething = async () => {
  throw new Error(`Error from doSomething`)
}

main()

The issue arose because the function, though marked as async, didn’t actually perform any asynchronous operations, making the async designation unnecessary. To ensure errors in async functions are captured by try-catch blocks, these functions must be invoked with an await. Alternatively, I could also remove the async from doSomething function. This way I don't need to use await when calling the function.

You might think that it's so silly to make this mistake. But mind you that the code in the repository had more lines and more complex.


As I pointed out at the beginning, the crash was identified during PR Review. This highlights the advantage of crafting an effective pull request that aids reviewers in both comprehending and testing the PR. For more insights on how to create and review pull requests, take a look at my book!