Last active
August 24, 2017 17:01
-
-
Save jonbodner/5911368fed3529852944b4665045321d to your computer and use it in GitHub Desktop.
future-blog-post-4
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
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