Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:52
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/2f12feae4bd00beffefce633ee5ec6b1 to your computer and use it in GitHub Desktop.
Save jonbodner/2f12feae4bd00beffefce633ee5ec6b1 to your computer and use it in GitHub Desktop.
future-blog-post-16
func main() {
a := 10
f := future.New(func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
}).Then(func(v interface{}) (interface{}, error) {
return doAnotherThing(v.(int))
})
go func() {
time.Sleep(1 * time.Second)
fmt.Println(“cancelling f!”)
f.Cancel()
}()
val, err := f.Get()
fmt.Println(val, err, f.IsCancelled())
g := future.New(func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
}).Then(func(v interface{}) (interface{}, error) {
return doAnotherThing(v.(int))
})
go func() {
time.Sleep(3 * time.Second)
fmt.Println(“Cancelling g!”)
g.Cancel()
}()
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