Created
August 24, 2017 16:35
-
-
Save jonbodner/56831b06e958adb7a37a57a6d69a2c97 to your computer and use it in GitHub Desktop.
future-blog-post-12
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)) | |
}) | |
// this will wait for a second to complete | |
// and will return nil for both val and err | |
// and timeout will be true | |
val, timeout, err := f.GetUntil(1 * time.Second) | |
fmt.Println(val, timeout, err) | |
// this will wait for val and err to have values | |
val, err = f.Get() | |
fmt.Println(val, err) | |
// this will error out on the first step, so we | |
// don’t do the long-running thing next | |
g := future.New(func() (interface{}, error) { | |
return doAnError(a) | |
}).Then(func(v interface{}) (interface{}, error) { | |
return doSomethingThatTakesAWhile(v.(int)) | |
}) | |
val2, timeout2, err2 := g.GetUntil(1 * time.Second) | |
fmt.Println(val2, timeout2, err2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment