Created
August 24, 2017 16:52
-
-
Save jonbodner/2f12feae4bd00beffefce633ee5ec6b1 to your computer and use it in GitHub Desktop.
future-blog-post-16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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