Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:35
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/56831b06e958adb7a37a57a6d69a2c97 to your computer and use it in GitHub Desktop.
Save jonbodner/56831b06e958adb7a37a57a6d69a2c97 to your computer and use it in GitHub Desktop.
future-blog-post-12
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