Documentation
¶
Index ¶
- Variables
- func AwaitAll[R any](ctx context.Context, futures ...Future[R]) func(yield func(int, result.Result[R]) bool)
- func AwaitAllAny(ctx context.Context, futures ...AnyFuture) func(yield func(int, result.Result[any]) bool)
- func AwaitAllResults[R any](ctx context.Context, futures ...Future[R]) []result.Result[R]
- func AwaitAllResultsAny(ctx context.Context, futures ...AnyFuture) []result.Result[any]
- func AwaitAllValues[R any](ctx context.Context, futures ...Future[R]) ([]R, error)
- func AwaitAllValuesAny(ctx context.Context, futures ...AnyFuture) ([]any, error)
- func AwaitFirst[R any](ctx context.Context, futures ...Future[R]) (R, error)
- func AwaitFirstAny(ctx context.Context, futures ...AnyFuture) (any, error)
- func New[R any]() (Promise[R], Future[R])
- type AnyFuture
- type Future
- type Memoizer
- type Promise
Constants ¶
This section is empty.
Variables ¶
var ErrNoResult = errors.New("no result")
ErrNoResult is returned when a future completes but has no defined result value.
var ErrNotReady = errors.New("future not ready")
ErrNotReady is returned when a future is not complete.
Functions ¶
func AwaitAll ¶ added in v0.0.2
func AwaitAll[R any](ctx context.Context, futures ...Future[R]) func(yield func(int, result.Result[R]) bool)
AwaitAll returns a function that yields the results of all futures. If the context is canceled, it returns an error for the remaining futures.
func AwaitAllAny ¶ added in v0.0.2
func AwaitAllAny(ctx context.Context, futures ...AnyFuture) func(yield func(int, result.Result[any]) bool)
AwaitAllAny returns a function that yields the results of all futures. If the context is canceled, it returns an error for the remaining futures.
func AwaitAllResults ¶ added in v0.0.2
AwaitAllResults waits for all futures to complete and returns the results. If the context is canceled, it returns early with errors for the remaining futures.
func AwaitAllResultsAny ¶ added in v0.0.2
AwaitAllResultsAny waits for all futures to complete and returns the results. If the context is canceled, it returns early with errors for the remaining futures.
func AwaitAllValues ¶ added in v0.0.2
AwaitAllValues returns the values of completed futures. If any future fails or the context is canceled, it returns early with an error.
func AwaitAllValuesAny ¶ added in v0.0.2
AwaitAllValuesAny returns the values of completed futures. If any future fails or the context is canceled, it returns early with an error.
func AwaitFirst ¶ added in v0.0.2
AwaitFirst returns the result of the first completed future. If the context is canceled, it returns early with an error.
func AwaitFirstAny ¶ added in v0.0.2
AwaitFirstAny returns the result of the first completed future. If the context is canceled, it returns early with an error.
Types ¶
type AnyFuture ¶ added in v0.0.2
type AnyFuture interface {
// contains filtered or unexported methods
}
AnyFuture matches a Future of any type.
type Future ¶
Future represents an asynchronous operation that will complete sometime in the future.
It is a read-only channel that can be used with Future.Await to retrieve the final result of a Promise.
func NewAsync ¶
NewAsync runs fn asynchronously, immediately returning a Future that can be used to retrieve the eventual result. This allows separating evaluating the result from computation.
func (Future[R]) Await ¶
Await returns the final result of the associated Promise. It can only be called once and blocks until a result is received or the context is canceled. If you need to read multiple times from a Future wrap it with Future.Memoize.
func (Future[R]) Memoize ¶
Memoize returns a memoizer for the given future, consuming it in the process.
The Memoizer can be queried multiple times from multiple goroutines.
func (Future[R]) Try ¶
Try returns the result when ready, ErrNotReady otherwise.
type Memoizer ¶
type Memoizer[R any] struct { // contains filtered or unexported fields }
A Memoizer is created with Future.Memoize and contains a memoized result of a future.
func (*Memoizer[R]) Try ¶
Try returns the result of the future if it is ready, otherwise it returns ErrNoResult.
type Promise ¶
Promise is used to send the result of an asynchronous operation.
It is a write-only promise. Either Promise.Resolve or Promise.Reject should be called exactly once.