Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:39
Show Gist options
  • Save jonbodner/88d9859f9c9f9b65def9c6c945a353e4 to your computer and use it in GitHub Desktop.
Save jonbodner/88d9859f9c9f9b65def9c6c945a353e4 to your computer and use it in GitHub Desktop.
future-blog-post-13
// Interface represents a future. No concrete implementation is
// exposed; all access to a future is via this interface.
type Interface interface {
// Get returns the values calculated by the future. It will pause until
// the value is calculated.
//
// If Get is invoked multiple times, the same value will be returned each time.
// Subsequent calls to Get will return instantaneously.
//
// When the future is cancelled, nil is returned for both the value and the error.
Get() (interface{}, error)
// GetUntil waits for up to Duration d for the future to complete. If the
// future completes before the Duration completes, the value and error are returned
// and timeout is returned as false. If the Duration completes before the future
// returns, nil is returned for the value and the error and timeout is returned
// as true.
//
// When the future is cancelled, nil is returned for both the value and the error.
GetUntil(d time.Duration) (interface{}, bool, error)
// Then allows multiple function calls to be chained together into a
// single future.
//
// Each call is run in order, with the output of the previous call passed into
// the next function in the chain. If an error occurs at any step in the chain,
// processing ceases and the error is returned via Get or GetUntil.
Then(Step) Interface
// Cancel prevents a future that hasn’t completed from returning a
// value. Any current or future calls to Get or GetUntil will return
// immediately.
//
// If the future has already completed or has already been
// cancelled, calling Cancel will do nothing.
// After a successful cancel, IsCancelled returns true.
//
// Calling Cancel on a future that has not completed does not stop the
// currently running function. However, any chained functions will not
// be run and the values returned by the current function are not accessible.
Cancel()
// IsCancelled indicates if a future terminated due to cancellation.
// If Cancel was called and the future’s work was not completed, IsCancelled
// returns true. Otherwise, it returns false
IsCancelled() bool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment