Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbodner/9616df776b352c282481fa9b400a5f7d to your computer and use it in GitHub Desktop.
Save jonbodner/9616df776b352c282481fa9b400a5f7d to your computer and use it in GitHub Desktop.
future-blog-post-18
func main() {
c, _ := context.WithTimeout(context.Background(), 1*time.Second)
a := 10
f := future.NewWithContext(c, func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
}).Then(func(v interface{}) (interface{}, error) {
return doAnotherThing(v.(int))
})
val, err := f.Get()
fmt.Println(val, err, f.IsCancelled())
c, _ = context.WithTimeout(context.Background(), 3*time.Second)
g := future.NewWithContext(c, func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
}).Then(func(v interface{}) (interface{}, error) {
return doAnotherThing(v.(int))
})
val2, err2 := g.Get()
fmt.Println(val2, err2, g.IsCancelled())
// once done happens, IsCancelled will never return true
// and Get still has the calculated values
time.Sleep(2 * time.Second)
val2, err2 = g.Get()
fmt.Println(val2, err2, g.IsCancelled())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment