jsutil

package
v0.0.0-...-613d1fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Rendered for js/wasm

Overview

Package jsutil contains various utility functions for working with JavaScript and WebAssembly

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AwaitPromise

func AwaitPromise(promise js.Value) (result js.Value, err error)

AwaitPromise accepts a js.Value representing a Promise. If the promise resolves, it returns (result, nil). If the promise rejects, it returns (js.Undefined, error). AwaitPromise has a synchronous-like API but does not block the JavaScript event loop.

func AwaitPromiseContext

func AwaitPromiseContext(ctx context.Context, promise js.Value) (result js.Value, err error)

AwaitPromiseContext is like AwaitPromise but accepts a context. If the context is canceled or times out before the promise resolves, it will return (js.Undefined, ctx.Error).

func CopyBytesToGo

func CopyBytesToGo(jsBytes js.Value) (bytes []byte, err error)

func CopyBytesToJS

func CopyBytesToJS(bytes []byte) (jsBytes js.Value, err error)

func ErrorToJS

func ErrorToJS(err error) js.Value

ErrorToJS converts a Go error to a JavaScript Error.

func InefficientlyConvertFromJS

func InefficientlyConvertFromJS(jsValue js.Value, value interface{}) (err error)

InefficientlyConvertFromJS converts the given JS value to a Go value and sets it. This function is not very efficient and its use should be phased out over time as much as possible.

func InefficientlyConvertToJS

func InefficientlyConvertToJS(value interface{}) (result js.Value, err error)

InefficientlyConvertToJS converts the given Go value to a JS value by encoding to JSON and then decoding it. This function is not very efficient and its use should be phased out over time as much as possible.

func IsNullOrUndefined

func IsNullOrUndefined(value js.Value) bool

IsNullOrUndefined returns true if the given JavaScript value is either null or undefined.

func RecoverError

func RecoverError(e interface{}) error

RecoverError allows a function to recover from a thrown Javascript error if called inside of a deferred function with a recover statement.

func WrapInPromise

func WrapInPromise(f func() (interface{}, error)) js.Value

WrapInPromise converts a potentially blocking Go function to a non-blocking JavaScript Promise. If the function returns an error, the promise will reject with that error. Otherwise, the promise will resolve with the first return value.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL