Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Last active August 24, 2017 17:01
Show Gist options
  • Save jonbodner/5911368fed3529852944b4665045321d to your computer and use it in GitHub Desktop.
Save jonbodner/5911368fed3529852944b4665045321d to your computer and use it in GitHub Desktop.
future-blog-post-4
package main
import (
"time"
"fmt"
"future"
)
func doSomethingThatTakesAWhile(i int) (int, error) {
// what this does isn’t that important for the example
// but it’s really important in your program
time.Sleep(2*time.Second)
return i * 2, nil
}
func main() {
a := 10
f := future.New(func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
})
// this will take about 2 seconds to complete
val, err := f.Get()
fmt.Println(val, err)
// this will complete immediately
val, err = f.Get()
fmt.Println(val, err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment