Documentation
¶
Overview ¶
Package future provides implementations for futures & promises.
Each Future/Promise creates a goroutinue to execute each one (or multiple when Promises are chained), and a channel to block on results. These are cleaned up when the original Func calls complete. Note that panics are not recovered explicitly, but you can recover then in your Func blocks.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCanceled = context.Canceled
Returned when a Future has be canceled
var ErrTimeout = context.DeadlineExceeded
Returned when a Future has timed out
Functions ¶
This section is empty.
Types ¶
type Future ¶
type Future interface { // Blocks on Future awaiting result Get() (Value, error) // Blocks on Future awaiting result, but returns a ErrTimeout if // the timeout Duration is hit before result returns. // Note that the execution still continues in Future after timeout. GetWithTimeout(timeout time.Duration) (Value, error) // Blocks on Future awaiting result as well as provided Context. // Execution continues in Future, even if Context hits deadline // or is canceled. GetWithContext(context.Context) (Value, error) }
A Future is a result to an asynchronous call that cal be blocked on for a result when needed.
Example ¶
package main import ( "fmt" "net/http" "github.com/sentientmonkey/future" ) func main() { f := future.NewFuture(func() (future.Value, error) { return http.Get("http://golang.org/") }) result, err := f.Get() if err != nil { fmt.Printf("Got error: %s\n", err) return } response := result.(*http.Response) defer response.Body.Close() fmt.Printf("Got result: %d\n", response.StatusCode) }
Output: Got result: 200
type Promise ¶
type Promise interface { // Block on Promise awaiting result chain Get() (Value, error) // Then adds an additional async function and return a new Promise. // The result from the previous promise is passed along as value. // Func is not invoked if a previous Promise returns an error. Then(Func func(value Value) (Value, error)) Promise }
Promises are like Futures, but provide the additional functionality of being chained together.
Example ¶
package main import ( "errors" "fmt" "io/ioutil" "net/http" "regexp" "github.com/sentientmonkey/future" ) func main() { p := future.NewPromise(func() (future.Value, error) { return http.Get("http://golang.org/") }) p = p.Then(func(value future.Value) (future.Value, error) { response := value.(*http.Response) defer response.Body.Close() b, err := ioutil.ReadAll(response.Body) if err != nil { return nil, err } return string(b), nil }) p = p.Then(func(value future.Value) (future.Value, error) { body := value.(string) r, err := regexp.Compile("<title>(.*)</title>") if err != nil { return nil, err } match := r.FindStringSubmatch(body) if len(match) < 1 { return nil, errors.New("Title not found") } return match[1], nil }) result, err := p.Get() if err != nil { fmt.Printf("Got error: %s\n", err) return } s := result.(string) fmt.Printf("Got result: %s\n", s) }
Output: Got result: The Go Programming Language
func NewPromise ¶
Create a new Promise. Func is asynchronously called and is resolved with a Get() call at an end of a Promise chain.