async

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](ctx context.Context, promises ...Promise[T]) ([]T, error)

All takes a slice of promises and will await the result of all of the specified promises. If any promise should return an error, the whole result slice is nil.

func Race

func Race[T any](ctx context.Context, promises ...Promise[T]) (T, error)

Race races multiple promises and returns the first non-error value while canceling other goroutines.

Types

type Promise

type Promise[T any] interface {
	// Settled indicates if a call to Await will cause a blocking behavior, or
	// if the result will be immediately returned.
	Settled() bool

	// Await will cause the calling code to block and wait for the promise to
	// settle. Await MUST be able to be called by multiple goroutines and safely
	// deliver the same value/error to all waiting goroutines. Successive calls
	// to Await should continue to respond with the result even once the promise
	// is settled.
	Await(context.Context) (T, error)
}

Promise is an abstract representation of a value that might eventually be delivered.

func New

func New[T, K any](ctx context.Context, fn func(context.Context, K) (T, error), arg K) Promise[T]

func New0

func New0[T any](ctx context.Context, fn func(context.Context) (T, error)) Promise[T]

NewPromise wraps a function in a goroutine that will make the result of that function deliver its result to the holder of the promise.

func Resolve

func Resolve[T any](v T) Promise[T]

Resolve wraps a value in a promise that will always be immediately settled and return the provided value.

Jump to

Keyboard shortcuts

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