promise

package
v1.15.5 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any]() (Resolver[T], Promise[T])

New creates a new promise for value T. Returns a resolver and the promise.

Types

type Promise

type Promise[T any] interface {
	// Await blocks until the value is resolved or rejected.
	Await(context.Context) (T, error)
}

A promise for a future value.

func Map

func Map[A, B any](p Promise[A], transform func(A) B) Promise[B]

Map transforms the value of a promise with the provided function.

func MapError added in v1.15.0

func MapError[A any](p Promise[A], transform func(error) error) Promise[A]

MapError transforms the error of a rejected promise with the provided function.

type Resolver

type Resolver[T any] interface {
	// Resolve a promise. Unblocks all Await()s. Future calls of Await()
	// return the resolved value immediately.
	//
	// Only the first call to resolve (or reject) has an effect and
	// further calls are ignored.
	Resolve(T)

	// Reject a promise with an error.
	Reject(error)
}

Resolver can resolve or reject a promise. These methods are separate from 'Promise' to make it clear where the promise is resolved from.

Jump to

Keyboard shortcuts

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