promise

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package promise provides a Promise[T] type for transpiled async/await TypeScript code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Promise

type Promise[T any] struct {
	// contains filtered or unexported fields
}

Promise represents an asynchronous computation that produces a value of type T.

func All

func All[T any](promises ...*Promise[T]) *Promise[[]T]

All waits for all promises and returns their values.

func AllSlice

func AllSlice(items any) *Promise[[]any]

AllSlice resolves a mixed slice where elements may be *Promise[any] or plain values. Accepts []any or any typed slice (e.g., []*Promise[string]) via reflection.

func New

func New[T any](executor func(resolve func(T), reject func(error))) *Promise[T]

New creates a Promise from an executor function (like JS new Promise((resolve, reject) => ...)).

func Race

func Race[T any](promises ...*Promise[T]) *Promise[T]

Race returns the first promise to settle.

func Reject

func Reject[T any](err error) *Promise[T]

Reject creates an already-rejected Promise.

func Resolve

func Resolve[T any](value T) *Promise[T]

Resolve creates an already-resolved Promise.

func ThenFunc

func ThenFunc[T, U any](p *Promise[T], fn func(T) (U, error)) *Promise[U]

ThenFunc chains a callback, returning a new Promise (standalone form with error return).

func (*Promise[T]) Await

func (p *Promise[T]) Await() (T, error)

Await blocks until the promise resolves and returns the value and error.

func (*Promise[T]) Then

func (p *Promise[T]) Then(fn any) *Promise[any]

Then chains a callback on the promise (method form). The callback receives the resolved value and returns the new value. Flattens Promise returns like JS .then().

Jump to

Keyboard shortcuts

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