Documentation ¶
Overview ¶
This module implements a simple Async Futures for golang Usage: f := NewAsyncFuture(childCtx, func(ctx2 context.Context) (interface{}, error) { can do large async / non blocking work
return ... }
f.Ready() // can be checked for completion f.Get() .. will block till the given sub-routine returns
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAsyncFutureCanceled = fmt.Errorf("async future was canceled")
ErrAsyncFutureCanceled is returned when the async future is cancelled by invoking the cancel function on the context
Functions ¶
This section is empty.
Types ¶
type AsyncFuture ¶
An asynchronously completing future
func NewAsyncFuture ¶
func NewAsyncFuture(ctx context.Context, closure func(context.Context) (interface{}, error)) *AsyncFuture
Creates a new Async future, that will call the method `closure` and return the results from the execution of this method
func (*AsyncFuture) Get ¶
func (f *AsyncFuture) Get(ctx context.Context) (interface{}, error)
Returns results (interface{} or an error) OR blocks till the results are available. If context is cancelled while waiting for results, an ErrAsyncFutureCanceled is returned
func (*AsyncFuture) Ready ¶
func (f *AsyncFuture) Ready() bool
returns whether the future is completed
type Future ¶
type Future interface { // Returns true if the Future is ready and either a value or error is available. Once Ready returns True, Get should return immediately Ready() bool // Get is a potentially blocking call, that returns the asynchronously computed value or an error // If Get is called before Ready() returns True, then it will block till the future has been completed Get(ctx context.Context) (interface{}, error) }
Provides a Future API for asynchronous completion of tasks
type SyncFuture ¶
type SyncFuture struct {
// contains filtered or unexported fields
}
This is a synchronous future, where the values are available immediately on construction. This is used to maintain a synonymous API with both Async and Sync tasks
func NewSyncFuture ¶
func NewSyncFuture(val interface{}, err error) *SyncFuture
Creates a new "completed" future that matches the async computation api