@@ -21,7 +21,10 @@ func jsValue(val any) js.Value {
2121 switch valType .Kind () {
2222 case reflect .Struct :
2323 return FromObject (val )
24+ case reflect .Slice :
25+ return FromSlice (val )
2426 }
27+
2528 return js .ValueOf (val )
2629}
2730
@@ -32,10 +35,10 @@ func errValue(val any) error {
3235 return val .(error )
3336}
3437
35- // AsFunction convert a classic Go function to a function taking js arguments.
38+ // AsPromise convert a classic Go function to a function taking js arguments.
3639// arguments and return types must be types handled by this package
3740// function must return 2 variables, second one must be an error
38- func AsFunction (goFunc any ) func ( this js. Value , args []js. Value ) ( any , error ) {
41+ func AsPromise (goFunc any ) JsFunc {
3942 goFuncValue := reflect .ValueOf (goFunc )
4043 goFuncType := goFuncValue .Type ()
4144
@@ -51,7 +54,7 @@ func AsFunction(goFunc any) func(this js.Value, args []js.Value) (any, error) {
5154 panic ("function must return an error" )
5255 }
5356
54- return func (this js.Value , args []js.Value ) (any , error ) {
57+ return AsyncJsFunc ( func (this js.Value , args []js.Value ) (any , error ) {
5558 if len (args ) != len (goFuncArgs ) {
5659 return nil , fmt .Errorf ("invalid number of arguments, expected %d, got %d" , len (goFuncArgs ), len (args ))
5760 }
@@ -60,13 +63,13 @@ func AsFunction(goFunc any) func(this js.Value, args []js.Value) (any, error) {
6063 for i , argType := range goFuncArgs {
6164 arg , err := goValue (argType , args [i ])
6265 if err != nil {
63- return nil , fmt .Errorf ("invalid argument at index %d with type %s: %w" , i , argType .String (), err )
66+ return nil , fmt .Errorf ("invalid argument at index %d, expected type %s: %w" , i , argType .String (), err )
6467 }
6568 argValues [i ] = reflect .ValueOf (arg )
6669 }
6770
6871 returnValues := goFuncValue .Call (argValues )
6972
7073 return jsValue (returnValues [0 ].Interface ()), errValue (returnValues [1 ].Interface ())
71- }
74+ })
7275}
0 commit comments