A better way to call an async function inside an event handler

in ReScript is there a better way to call an async function inside an event handler than using ->ignore?

Need to call this async function inside an onClick but get compiler error unless I call it with ->ignore.

let handleDuplicateQuote = async () => {
      try {
        await mutation_duplicateQuote({quoteId: quote._id})
      } catch {
      | Js.Exn.Error(err) => Js.log2("Failure!!", err)
      }
    }

onClick

onClick={_ => { 
        handleDuplicateQuote()->ignore
 }}

I wonder if there is a better way

a safer way to ignore a promise if you use RescriptCore is to call Promise.done.

3 Likes

I have this utility function in my project:

// Turn async function into a fire-and-forget function
let prime: ('a => promise<unit>) => 'a => unit = f => x => f(x)->ignore

With that, I would handle this by wrapping the handler with it:

let handleDuplicateQuote = prime(async _ => ...)

then I can just use it like a normal event handler:

onClick=handleDuplicateQuote
4 Likes

In uncurried mode, doing this should be safe as well:

let _ = handleDuplicateQuote()
2 Likes

i’ve used this but ->ignore feels less hacky to me