how can i use js callback #1

Closed
opened 2022-10-10 11:30:26 +00:00 by liuxingpluck · 1 comment
liuxingpluck commented 2022-10-10 11:30:26 +00:00 (Migrated from github.com)

i want to use like this:
js code:
let gomodule=require("./gomod.node");
gomodule.func(
function( fromGoObj){
console.info(fromGoObj)
}
);

then how can i write go code?

Your project has been very helpful to me. Thank you very much

i want to use like this: js code: let gomodule=require("./gomod.node"); gomodule.func( function( fromGoObj){ console.info(fromGoObj) } ); then how can i write go code? Your project has been very helpful to me. Thank you very much
akshayganeshen commented 2022-11-07 22:40:07 +00:00 (Migrated from github.com)

Hey! Sorry for the super slow response on this.

I'm still developing the js_native_api.go to allow for this use-case, but it's not there just yet. Ultimately you'd need to make use of the napi_call_function / napi.CallFunction API to do this.

This isn't finalized, but the Go portion would look a little something like this once that's added:

func FuncCallback(env napi.Env, info napi.CallbackInfo) napi.Value {
  callbackInfo, _ := napi.GetCbInfo(env, info)
  jsCallback := callbackInfo.Args[0]

  fromGoObj, _ := napi.CreateObject(env)
  globalValue, _ := napi.GetGlobal(env)
  // this napi.CallFunction API does not exist yet:
  napi.CallFunction(env, globalValue, jsCallback, []napi.Value{fromGoObj})
}

// func CallFunction(env napi.Env, recv napi.Value, args []napi.Value) (napi.Value, napi.Status)
Hey! Sorry for the super slow response on this. I'm still developing the `js_native_api.go` to allow for this use-case, but it's not there just yet. Ultimately you'd need to make use of the `napi_call_function` / `napi.CallFunction` API to do this. This isn't finalized, but the Go portion would look a little something like this once that's added: ```go func FuncCallback(env napi.Env, info napi.CallbackInfo) napi.Value { callbackInfo, _ := napi.GetCbInfo(env, info) jsCallback := callbackInfo.Args[0] fromGoObj, _ := napi.CreateObject(env) globalValue, _ := napi.GetGlobal(env) // this napi.CallFunction API does not exist yet: napi.CallFunction(env, globalValue, jsCallback, []napi.Value{fromGoObj}) } // func CallFunction(env napi.Env, recv napi.Value, args []napi.Value) (napi.Value, napi.Status) ```
Sign in to join this conversation.
No description provided.