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
func (f *futureImpl) Then(next Step) Interface { | |
nextFuture := New(func() (interface{}, error) { | |
result, err := f.Get() | |
if err != nil { | |
return result, err | |
} | |
return next(result) | |
}) | |
return nextFuture | |
} |
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
// 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. | |
Get() (interface{}, error) |
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
func main() { | |
a := 10 | |
f := future.New(func() (interface{}, error) { | |
return doSomethingThatTakesAWhile(a) | |
}) | |
// this will wait for a second to complete | |
// and will return nil for both val and err | |
// and timeout will be true | |
val, timeout, err := f.GetUntil(1 * time.Second) |
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
func (f *futureImpl) GetUntil(d time.Duration) (interface{}, bool, error) { | |
select { | |
case <-f.done: | |
val, err := f.Get() | |
return val, false, err | |
case <-time.After(d): | |
return nil, true, nil | |
} | |
// This should never be executed |
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
// 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. | |
Get() (interface{}, error) |
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
// New creates a new Future that wraps the provided function. | |
func New(inFunc Processor) Interface { | |
f := &futureImpl { | |
done: make(chan struct{}), | |
} | |
go func() { | |
f.val, f.err = inFunc() | |
close(f.done) | |
}() |
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
type futureImpl struct { | |
done chan struct{} | |
val interface{} | |
err error | |
} | |
func (f *futureImpl) Get() (interface{}, error) { | |
<-f.done | |
return f.val, f.err | |
} |
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 main | |
import ( | |
"time" | |
"fmt" | |
"future" | |
) | |
func doSomethingThatTakesAWhile(i int) (int, error) { | |
// what this does isn’t that important for the example |
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
func New(inFunc Process) Interface { | |
//… | |
} |
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
type Process func() (interface{}, error) |