Promise returned in function argument where a void return was expected

0

Issue

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?

Solution

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.

Answered By – Lionel Rowe

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More