Documentation ¶
Overview ¶
package futures provides a Future type which can be used to model the result of a ongoing computation which could fail.
Futures are not the idiomatic way to deal with concurrency in Go. Go APIs should be synchronous not asynchronous. If your API returns a Future: you are doing it wrong. That being said, The network *is* asynchronous, futures, especially the Promises, provide a way to build synchronous APIs on top of the asynchronous network.
Index ¶
- func Await[T any](ctx context.Context, f Future[T]) (T, error)
- func Await2[A, B any](ctx context.Context, af Future[A], bf Future[B]) (retA A, retB B, _ error)
- func Await3[A, B, C any](ctx context.Context, af Future[A], bf Future[B], cf Future[C]) (retA A, retB B, retC C, _ error)
- func IsFailure[T any](f Future[T]) bool
- func IsSuccess[T any](f Future[T]) bool
- type Future
- func CollectSlice[T any](futs []Future[T]) Future[[]T]
- func Go[T any](fn func() (T, error)) Future[T]
- func Join2[A, B, Z any](afut Future[A], bfut Future[B], fn func(A, B) Z) Future[Z]
- func Join3[A, B, C, Z any](a Future[A], b Future[B], c Future[C], fn func(A, B, C) Z) Future[Z]
- func Join4[A, B, C, D, Z any](a Future[A], b Future[B], c Future[C], d Future[D], fn func(A, B, C, D) Z) Future[Z]
- func Map[A, Z any](x Future[A], fn func(A) Z) Future[Z]
- func NewFailure[T any](err error) Future[T]
- func NewSuccess[T any](x T) Future[T]
- type Promise
- type Store
- func (s *Store[K, V]) Delete(k K, p *Promise[V])
- func (s *Store[K, V]) Fail(k K, err error)
- func (s *Store[K, V]) ForEach(fn func(k K, v *Promise[V]) bool) bool
- func (s *Store[K, V]) Get(k K) *Promise[V]
- func (s *Store[K, V]) GetOrCreate(k K) (*Promise[V], bool)
- func (s *Store[K, V]) Succeed(k K, x V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Future ¶
type Future[T any] interface { // IsDone returns whether the future is done. It does not block. IsDone() bool // contains filtered or unexported methods }
func CollectSlice ¶
CollectSlice converts a slice of Futures of type T to a single future of a slice of type T.
func Join2 ¶
Join2 takes 2 futures of different types and a merging function fn. Join2 returns a future containing the result of fn(a, b), where a is the value in afut, and b is the value in bfut
func NewFailure ¶
NewFailure returns a future which has already failed with err
func NewSuccess ¶
NewSuccess returns a future which has already succeeded with x
type Promise ¶
type Promise[T any] struct { // contains filtered or unexported fields }
func NewPromise ¶
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
func NewStore ¶
func NewStore[K comparable, V any]() *Store[K, V]