Skip to content

proposal: syscall/js: AsyncFuncOf #72045

@jcbhmr

Description

@jcbhmr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions