🎉 Commit of go-promises

This commit is contained in:
Dan Jones 2024-12-27 15:19:49 -06:00
commit 5d84d26c3a
Signed by: dan
GPG key ID: 5B3B0F7217473A29
13 changed files with 238 additions and 0 deletions

View file

@ -0,0 +1,14 @@
package main
import (
"fmt"
"net/http"
)
func main() {
err := http.ListenAndServe(":9090", http.FileServer(http.Dir("./internal/assets")))
if err != nil {
fmt.Println("Failed to start server", err)
return
}
}

50
internal/cmd/wasm/main.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"errors"
"fmt"
"syscall/js"
promises "codeberg.org/danjones000/go-promises"
"codeberg.org/danjones000/go-promises/internal"
)
func main() {
fmt.Println("Some go")
js.Global().Set("add", promises.PromisifyGoFunc(func(_ js.Value, args []js.Value) (any, error) {
if len(args) < 2 {
return nil, errors.New("add called with too few arguments")
}
a := args[0]
b := args[1]
if a.Type() != js.TypeNumber {
return nil, fmt.Errorf("First argument must be a number. %s given", a.Type())
}
if b.Type() != js.TypeNumber {
return nil, fmt.Errorf("secong argument must be a number. %s given", b.Type())
}
return internal.Add(a.Int(), b.Int()), nil
}))
js.Global().Set("getUser", promises.PromisifyGoFunc(func(this js.Value, args []js.Value) (any, error) {
if len(args) < 2 {
return nil, errors.New("getUser called with too few arguments")
}
name := args[0]
age := args[1]
if name.Type() != js.TypeString {
return nil, fmt.Errorf("First argument must be a string. %s given", name.Type())
}
if age.Type() != js.TypeNumber {
return nil, fmt.Errorf("Second argument must be a string. %s given", age.Type())
}
return internal.GetUser(name.String(), age.Int()), nil
}))
<-make(chan struct{})
}