Documentation ¶
Index ¶
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 ¶
Types ¶
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 List ¶
List is a list of Future, representing results of asynchronous tasks.
func (List[R]) All ¶
All returns a function that yields the results of all futures. If the context is canceled, it returns an error for the remaining futures.
func (List[R]) AwaitAll ¶
AwaitAll 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 (List[R]) AwaitAllValues ¶
AwaitAllValues returns the values of completed futures. If any future fails or the context is canceled, it returns early with an error.
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.