#Can on__ handler be async?

9 messages · Page 1 of 1 (latest)

teal dock
#

Seems that having async handlers works fine,

const button = <button onClick={doSomethingAsync}>Click me</button>

although the particular ESLint config I'm working with doesn't like it,

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

Are async handlers supported? If so, do Solid's types need to be updated?

#

Possibly updated the following?

  interface EventHandler<T, E extends Event> {
    (
      e: E & {
        currentTarget: T;
        target: DOMElement;
      }
-   ): void;
+   ): Promise<void> | void
  }
hollow condor
#

you can use promises
and solid types do not need to be updated
because void already means that it doesn’t care about what you return

#

this seems like an issue with your eslint rule, not typescript

teal dock
#

Thanks for clarifying. Does void really mean that it doesn't care about the return value? Isn't it more like it doesn't expect a return value?

hollow condor
#

yeah you can theoretically return anything and typescript won't complain (which is why an eslint rule might be useful to catch that, as it is weird do do that)
but ts won't let you use the value returned by that function

#

its a weird design
but it means that any function will extend a (...a: any[]) => void type

teal dock
#

Got it, thanks!

muted trout
#

If the async thing you do is a request, it might be better to just use a signal's setter as event handler and use a derived version of the getter as source for a resource.