Skip to content

Instantly share code, notes, and snippets.

@sadhasivam
Forked from davecheney/future.go
Created May 11, 2016 04:00
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 sadhasivam/6f2bee48ffa724a35f33d61347de7059 to your computer and use it in GitHub Desktop.
Save sadhasivam/6f2bee48ffa724a35f33d61347de7059 to your computer and use it in GitHub Desktop.
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