Documentation ¶
Overview ¶
Package async provides utilities for handling asynchronous operations and results.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotReady = errors.New("future not ready")
Functions ¶
Types ¶
type Awaitable ¶
Awaitable is the underlying interface for Future and Memoizer. It blocks until a result is received or the context is canceled. Plain futures can only be queried once, while memoizers can be queried multiple times.
type Future ¶
Future represents an asynchronous operation that will complete sometime in the future.
It is a read-only channel that can be used to retrieve the final result of a Promise with Future.Wait.
func NewAsyncFuture ¶
NewAsyncFuture runs f asynchronously, immediately returning a Future that can be used to retrieve the eventual result. This allows separating evaluating the result from computation.
func NewFuture ¶
NewFuture provides a simple way to create a Future for synchronous operations. This allows synchronous and asynchronous code to be composed seamlessly and separating initiation from waiting.
- f takes a func that accepts a Promise as a Promise
The returned Future that can be used to retrieve the eventual result of the Promise.
func ThenAsync ¶
ThenAsync asynchronously transforms the embedded result from an Awaitable using 'then'.
func (Future[R]) Wait ¶
Wait 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.
type Memoizer ¶
type Memoizer[R any] interface { Awaitable[R] // TryWait returns the cached result when ready, [ErrNotReady] otherwise. TryWait() (R, error) }
Memoizer caches results from a Future to enable multiple queries and avoid unnecessary recomputation.
type Promise ¶
Promise is used to send the result of an asynchronous operation.
It is a write-only channel. Either Promise.SendValue or Promise.SendError should be called exactly once.