futures

package
v0.0.0-...-cb92b4e Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: MPL-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Await

func Await[T any](ctx context.Context, f Future[T]) (T, error)

Await blocks until the future is complete then returns the result.

func Await2

func Await2[A, B any](ctx context.Context, af Future[A], bf Future[B]) (retA A, retB B, _ error)

func Await3

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

func IsFailure[T any](f Future[T]) bool

func IsSuccess

func IsSuccess[T any](f Future[T]) bool

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

func CollectSlice[T any](futs []Future[T]) Future[[]T]

CollectSlice converts a slice of Futures of type T to a single future of a slice of type T.

func Go

func Go[T any](fn func() (T, error)) Future[T]

Go spawns fn in a separate Goroutine and returns a Future for it's completion

func Join2

func Join2[A, B, Z any](afut Future[A], bfut Future[B], fn func(A, B) Z) Future[Z]

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 Join3

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

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

func Map[A, Z any](x Future[A], fn func(A) Z) Future[Z]

func NewFailure

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

NewFailure returns a future which has already failed with err

func NewSuccess

func NewSuccess[T any](x T) Future[T]

NewSuccess returns a future which has already succeeded with x

type Promise

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

func NewPromise

func NewPromise[T any]() *Promise[T]

func (*Promise[T]) Fail

func (f *Promise[T]) Fail(err error) (ret bool)

func (*Promise[T]) IsDone

func (f *Promise[T]) IsDone() bool

func (*Promise[T]) IsFailure

func (f *Promise[T]) IsFailure() bool

func (*Promise[T]) IsSuccess

func (f *Promise[T]) IsSuccess() bool

func (*Promise[T]) Succeed

func (f *Promise[T]) Succeed(x T) (ret bool)

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]

func (*Store[K, V]) Delete

func (s *Store[K, V]) Delete(k K, p *Promise[V])

func (*Store[K, V]) Fail

func (s *Store[K, V]) Fail(k K, err error)

func (*Store[K, V]) ForEach

func (s *Store[K, V]) ForEach(fn func(k K, v *Promise[V]) bool) bool

func (*Store[K, V]) Get

func (s *Store[K, V]) Get(k K) *Promise[V]

func (*Store[K, V]) GetOrCreate

func (s *Store[K, V]) GetOrCreate(k K) (*Promise[V], bool)

func (*Store[K, V]) Succeed

func (s *Store[K, V]) Succeed(k K, x V)

Jump to

Keyboard shortcuts

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