Created
August 24, 2017 16:58
-
-
Save jonbodner/9616df776b352c282481fa9b400a5f7d to your computer and use it in GitHub Desktop.
future-blog-post-18
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() { | |
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