@@ -6,32 +6,28 @@ import "sync"
66// but will be available at some point, or an error if that value could not be made available.
77type Future interface {
88
9- // Creates a new future by applying a function to the successful result of this future .
9+ // Map creates a new Future by applying a function to the successful result of this Future .
1010 Map (func (interface {}) (interface {}, error )) Future
1111
12- // Creates a new future by applying a function to the successful result of
13- // this future, and returns the result of the function as the new future .
12+ // FlatMap creates a new Future by applying a function to the successful result of
13+ // this Future .
1414 FlatMap (func (interface {}) (Future , error )) Future
1515
16- // Blocks until Future completed and return either result or error.
16+ // Get blocks until the Future is completed and returns either a result or an error.
1717 Get () (interface {}, error )
1818
19- // Creates a new future that will handle any error that this
20- // future might contain. If this future contains
21- // a valid result then the new future will contain the same.
19+ // Recover handles any error that this Future might contain using a resolver function.
2220 Recover (func () (interface {}, error )) Future
2321
24- // Creates a new future that will handle any error that this
25- // future might contain by assigning it a value of another future.
26- // If this future contains a valid result then the new future will contain the same result.
22+ // RecoverWith handles any error that this Future might contain using another Future.
2723 RecoverWith (Future ) Future
2824
29- // Complete future with either result or error
30- // For Promise use internally
25+ // complete completes the Future with either a value or an error.
26+ // Is used by Promise internally.
3127 complete (interface {}, error )
3228}
3329
34- // FutureImpl Future implementation
30+ // FutureImpl implements the Future interface.
3531type FutureImpl struct {
3632 acc sync.Once
3733 compl sync.Once
@@ -40,14 +36,14 @@ type FutureImpl struct {
4036 err error
4137}
4238
43- // NewFuture returns new Future
39+ // NewFuture returns a new Future.
4440func NewFuture () Future {
4541 return & FutureImpl {
4642 done : make (chan interface {}),
4743 }
4844}
4945
50- // accept blocks once until result is available
46+ // accept blocks once, until the Future result is available.
5147func (fut * FutureImpl ) accept () {
5248 fut .acc .Do (func () {
5349 sig := <- fut .done
@@ -60,7 +56,8 @@ func (fut *FutureImpl) accept() {
6056 })
6157}
6258
63- // Map default implementation
59+ // Map creates a new Future by applying a function to the successful result of this Future
60+ // and returns the result of the function as a new Future.
6461func (fut * FutureImpl ) Map (f func (interface {}) (interface {}, error )) Future {
6562 next := NewFuture ()
6663 go func () {
@@ -74,7 +71,8 @@ func (fut *FutureImpl) Map(f func(interface{}) (interface{}, error)) Future {
7471 return next
7572}
7673
77- // FlatMap default implementation
74+ // FlatMap creates a new Future by applying a function to the successful result of
75+ // this Future and returns the result of the function as a new Future.
7876func (fut * FutureImpl ) FlatMap (f func (interface {}) (Future , error )) Future {
7977 next := NewFuture ()
8078 go func () {
@@ -93,35 +91,43 @@ func (fut *FutureImpl) FlatMap(f func(interface{}) (Future, error)) Future {
9391 return next
9492}
9593
96- // Get default implementation
94+ // Get blocks until the Future is completed and returns either a result or an error.
9795func (fut * FutureImpl ) Get () (interface {}, error ) {
9896 fut .accept ()
9997 return fut .value , fut .err
10098}
10199
102- // Recover default implementation
100+ // Recover handles any error that this Future might contain using a given resolver function.
101+ // Returns the result as a new Future.
103102func (fut * FutureImpl ) Recover (f func () (interface {}, error )) Future {
104- fut .accept ()
105- if fut .err != nil {
106- next := NewFuture ()
107- next .complete (f ())
108- return next
109- }
110- return fut
103+ next := NewFuture ()
104+ go func () {
105+ fut .accept ()
106+ if fut .err != nil {
107+ next .complete (f ())
108+ } else {
109+ next .complete (fut .value , nil )
110+ }
111+ }()
112+ return next
111113}
112114
113- // RecoverWith default implementation
115+ // RecoverWith handles any error that this Future might contain using another Future.
116+ // Returns the result as a new Future.
114117func (fut * FutureImpl ) RecoverWith (rf Future ) Future {
115- fut .accept ()
116- if fut .err != nil {
117- next := NewFuture ()
118- next .complete (rf .Get ())
119- return next
120- }
121- return fut
118+ next := NewFuture ()
119+ go func () {
120+ fut .accept ()
121+ if fut .err != nil {
122+ next .complete (rf .Get ())
123+ } else {
124+ next .complete (fut .value , nil )
125+ }
126+ }()
127+ return next
122128}
123129
124- // complete future with either value or error
130+ // complete completes the Future with either a value or an error.
125131func (fut * FutureImpl ) complete (v interface {}, e error ) {
126132 fut .compl .Do (func () {
127133 go func () {
0 commit comments