Proposal Details
Let you do this:
jsFunction := js.FuncOf(func(_ js.Value, args []js.Value) any {
return args[0].Float() + args[1].Float()
})
jsAsyncFunction := js.AsyncFuncOf(func(_ js.Value, args []js.Value) any {
time.Sleep(100 * time.Millisecond)
return args[0].Float() + args[1].Float()
})
How you could do the same thing today without js.AsyncFuncOf():
// disclaimer: I have no idea if this code works or not
jsAsyncFunction := js.FuncOf(func(_ js.Value, args []js.Value) any {
executor := js.FuncOf(func(_ js.Value, executorArgs []js.Value) any {
resolve := executorArgs[0]
reject := executorArgs[1]
go func() {
defer func() {
if r := recover(); r != nil {
reject.Invoke(r)
}
}()
time.Sleep(100 * time.Millisecond)
resolve.Invoke(args[0].Float() + args[1].Float())
}()
return js.Value{}
})
defer executor.Release()
return js.Global().Get("Promise").New(executor)
})
...but that's quite a lot of code.
Also this would correspond very nicely with the ()=>{} & async ()=>{} counterparts. ()=>{} => js.FuncOf() and async ()=>{} => js.AsyncFuncOf(). Nice and tidy.
This idea was referenced already: #69720
Proposal Details
Let you do this:
How you could do the same thing today without
js.AsyncFuncOf():...but that's quite a lot of code.
Also this would correspond very nicely with the
()=>{}&async ()=>{}counterparts.()=>{}=>js.FuncOf()andasync ()=>{}=>js.AsyncFuncOf(). Nice and tidy.This idea was referenced already: #69720