Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint reports "Promise-returning function provided to attribute where a void return was expected" [duplicate]

I have a React application that is giving a linting error:

Promise-returning function provided to attribute
where a void return was expected
.eslint@typescript-eslint/no-misused-promises

The function looks like this

      <Button onClick={
        async () => {
          await handleDelete()
        }}
      >

I've tried adding return void along with a number of different ways to wrap the code and add catch as suggested in various SO posts but none of them worked to resolve the error.

I do not wish to disable the rule without understanding why - and once I do it will probably be to remove or disable the rule in the lint config.

like image 214
Michael Durrant Avatar asked Oct 28 '25 14:10

Michael Durrant


1 Answers

There are two approaches here:

  1. You could either wrap the function in an IIFE (Immediately Invoked Function Expression),
     <Button onClick={
         () => {
             void (async () => {
                 await handleDelete()
             })();
          }
      }>

You can learn more about IIFE on: MDN Official Docs

  1. Or, you could disable the no-misused-promises rule.

For project wide, add rule in .eslintrc:

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

To disable this inline, comment this above the particular occurrence to ignore the error.

// eslint-disable-next-line @typescript-eslint/no-misused-promises

You could read about it more on: GitHub ESLint Docs

like image 120
D V Avatar answered Oct 31 '25 05:10

D V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!