-
-
Save toffaletti/2d5e0205097005c67afa to your computer and use it in GitHub Desktop.
This file contains 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 future | |
// A Future represents the result of some asynchronous computation. | |
// Future returns the result of the work as an error, or nil if the work | |
// was performed successfully. | |
// Implementers must observe these invariants | |
// 1. There may be multiple concurrent callers, or Future may be called many | |
// times in sequence, it must always return the same value. | |
// 2. Future blocks until the work has been performed. | |
type Future func() error | |
// New returns a Future representing the result of the execution of f. | |
func New(f func() error) Future { | |
ch := make(chan error, 1) | |
go func() { | |
ch <- f() | |
}() | |
return func() error { | |
result := <-ch | |
ch <- result | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment