Promise returned in function argument where a void return was expected

Viewed 23505

I'm working on an Electron application and I want to use async await in an anonymous function in my Main like this:

process.on("uncaughtException", async (error: Error) => {
  await this.errorHandler(error);
});

But this yields the Typescript error

Promise returned in function argument where a void return was expected.

I'm using Typescript 3.9.7 and Electron 9.2.0.

Why doesn't it allow me to use async/await?

2 Answers

You can use an asynchronous IIFE inside the callback, like this:

process.on("uncaughtException", (error: Error) => {
  (async () => {
    await this.errorHandler(error);

    // ...
  })();
});

This ensures that the implicit return of the callback remains undefined, rather than being a promise.

You can turn off this check (not the entire rule) with the help of checksVoidReturn option:

.eslinter.js

rules: {
    {
      "@typescript-eslint/no-misused-promises": [
        "error",
        {
          "checksVoidReturn": false
        }
      ]
    }
}

But I strongly recommend not to do this and rewrite your code using .then and .catch to avoid hitting this linter error.

You may also disable this rule inline with // eslint-disable-next-line @typescript-eslint/no-misused-promises comment above this particular occurrence along with a few lines of comment describing why you think this is okay to ignore the error.

Related