func main() { f := future.NewFuture() go func() { v, err := f.GetOrTimeout(2 * time.Second) if err != nil { if ferr, ok := err.(future.FutureError); ok { println("Future error:", ferr.Error()) } else { println("Other error:", err.Error()) } } println("Got: ", v.(int)) }() // // Set the future value. // f.SetValue(1) // Allow it to print result. time.Sleep(1 * time.Second) }
func (c *CatServiceProviderLocal) RequestCat(name string) { f := future.NewFuture() acr := requestCatRequest{name, f, c} c.dec.Execute(acr.Run) val, err := f.Get() if err != nil { // Do something intelligent with it. } println("Cat:", name, val.(CatInfo).String()) }
// // Does an entry exist in the register. // func (r *Register) EntryExists(name string) bool { // // A future is used to block the caller. // fture := future.NewFuture() task := registerExistsTask{r, fture, name} r.dec.Execute(task.Run) // Await the response indefinitely. v, err := fture.Get() if err != nil { println(err.Error()) return false // You need to consider how errors are handled here, it can require some thought! } return v.(bool) }
func (r *Register) Count() int { _future := future.NewFuture() // // Simple inner function. // r.dec.Execute(func() (executor.ExecutionResult, error) { // // Update if exists. // _future.SetValue(len(r.data)) return executor.EX_OK, nil }) v, e := _future.Get() if e != nil { println(e.Error()) return -1 // sake of example. } return v.(int) }