future

package
v0.0.0-...-5d34579 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Future

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

Future represents a result that will be available at some point in the future. It is very similar to Rust's Future trait.

func After

func After[T any](fs ...Future[T]) Future[struct{}]

After returns a single future that resolves after all of the given futures. If any future errors, the returned future immediately resolves to an error. This is very similar to Join except that the resolved value will be empty (making it more efficient if you don't need the values from the joined futures).

func Err

func Err[T any](err error) Future[T]

Err returns a new future that is immediately ready with the given error.

func Join

func Join[T any](fs ...Future[T]) Future[[]T]

Join combines the values from multiple futures into a single future that resolves to []T. If any future errors, the returned future immediately resolves to an error.

func Map

func Map[T any, U any](f Future[T], fn func(Result[T]) Result[U]) Future[U]

Map converts a future's result to a different type using a conversion function.

func MapOk

func MapOk[T any, U any](f Future[T], fn func(T) U) Future[U]

MapOk converts a future's value to a different type using a conversion function.

func MapOkToAny

func MapOkToAny[T any](f Future[T]) Future[any]

MapOk converts a future's value to an `any` type.

func MapOkValue

func MapOkValue[T any, U any](f Future[T], v U) Future[U]

MapOk converts a future's value to a value of a different type.

func New

func New[T any](poll func() (Result[T], bool)) Future[T]

New constructs a new future from a poll function. When the future's value is ready, poll should return the value and true. Otherwise, poll should return a zero value and false.

func Ok

func Ok[T any](v T) Future[T]

Ok returns a new future that is immediately ready with the given value.

func Then

func Then[T any, U any](f Future[T], fn func(Result[T]) Future[U]) Future[U]

Then invokes f when the future is resolved and returns a future that resolves when f's return value is resolved.

func (Future[T]) IsReady

func (f Future[T]) IsReady() bool

IsReady returns true if the future's value is ready.

func (*Future[T]) Poll

func (f *Future[T]) Poll()

Poll invokes pollers for the future and its dependencies, allowing futures to transition to the ready state.

func (Future[T]) Result

func (f Future[T]) Result() Result[T]

Result returns the future's result if it is ready.

type Result

type Result[T any] struct {
	Value T
	Error error
}

Result holds either a value or an error.

func (Result[T]) IsErr

func (r Result[T]) IsErr() bool

IsErr returns true if the result is an error.

func (Result[T]) IsOk

func (r Result[T]) IsOk() bool

IsOk returns true if the result is not an error.

Jump to

Keyboard shortcuts

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