await

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 1 Imported by: 0

README

tinywasm/await

Go Reference

Minimal JS async bridge for Go WASM: block a goroutine on a Promise or one-shot DOM event with zero dependencies.

Overview

In Go WebAssembly applications, blocking on JS Promises or DOM events requires registering callbacks (js.FuncOf), yielding via a channel, and cleaning up listeners afterwards. tinywasm/await provides clean, leak-free primitives with zero external dependencies.

Event is the shared underlying primitive that registers one-shot event listeners, handles resolution and error channels, and removes both listeners and releases callback references on return.

Installation

go get github.com/tinywasm/await

Note: All files require //go:build wasm and syscall/js.

Usage

Block on a JS Promise

await.Promise blocks until the js.Value Promise settles via .then or .catch.

package main

import (
	"syscall/js"

	"github.com/tinywasm/await"
)

func main() {
	p := js.Global().Get("fetch").Invoke("https://api.example.com/data")
	res, err := await.Promise(p)
	if err != nil {
		panic(err)
	}
	// res is the Response object
}
Block on an IndexedDB Request

await.Request blocks until an IndexedDB request fires "success" or "error", returning req.Get("result") on success or extracting the error message on failure.

package main

import (
	"syscall/js"

	"github.com/tinywasm/await"
)

func getRecord(store js.Value, key string) (js.Value, error) {
	req := store.Call("get", key)
	return await.Request(req)
}
Block on a One-Shot DOM Event

await.Event blocks on any js.Value target exposing addEventListener / removeEventListener until either the success event or error event fires.

package main

import (
	"syscall/js"

	"github.com/tinywasm/await"
)

func awaitLoad(element js.Value) (js.Value, error) {
	return await.Event(element, "load", "error")
}

License

MIT

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Event added in v0.1.0

func Event(target js.Value, okEvent, failEvent string) (js.Value, error)

Event blocks the calling goroutine until target fires exactly one of the named JS events, then removes both listeners. Safe in WASM: the channel receive yields the goroutine and the JS event loop keeps running.

func Promise added in v0.1.0

func Promise(p js.Value) (js.Value, error)

Promise blocks until p settles, via .then/.catch.

func Request added in v0.1.0

func Request(req js.Value) (js.Value, error)

Request blocks until an IndexedDB request fires "success" or "error", and returns req.Get("result") on success. It is Event with IndexedDB's specific error-message extraction (req.Get("error").Get("message")).

Types

type Error added in v0.1.0

type Error string

Error represents an error returned by the await package.

const (
	// ErrRejected wraps a Promise rejection with no usable message.
	ErrRejected Error = "await: promise rejected"
)

func (Error) Error added in v0.1.0

func (e Error) Error() string

Jump to

Keyboard shortcuts

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