@@ -6,32 +6,28 @@ import "sync"
6
6
// but will be available at some point, or an error if that value could not be made available.
7
7
type Future interface {
8
8
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 .
10
10
Map (func (interface {}) (interface {}, error )) Future
11
11
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 .
14
14
FlatMap (func (interface {}) (Future , error )) Future
15
15
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.
17
17
Get () (interface {}, error )
18
18
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.
22
20
Recover (func () (interface {}, error )) Future
23
21
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.
27
23
RecoverWith (Future ) Future
28
24
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.
31
27
complete (interface {}, error )
32
28
}
33
29
34
- // FutureImpl Future implementation
30
+ // FutureImpl implements the Future interface.
35
31
type FutureImpl struct {
36
32
acc sync.Once
37
33
compl sync.Once
@@ -40,14 +36,14 @@ type FutureImpl struct {
40
36
err error
41
37
}
42
38
43
- // NewFuture returns new Future
39
+ // NewFuture returns a new Future.
44
40
func NewFuture () Future {
45
41
return & FutureImpl {
46
42
done : make (chan interface {}),
47
43
}
48
44
}
49
45
50
- // accept blocks once until result is available
46
+ // accept blocks once, until the Future result is available.
51
47
func (fut * FutureImpl ) accept () {
52
48
fut .acc .Do (func () {
53
49
sig := <- fut .done
@@ -60,7 +56,8 @@ func (fut *FutureImpl) accept() {
60
56
})
61
57
}
62
58
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.
64
61
func (fut * FutureImpl ) Map (f func (interface {}) (interface {}, error )) Future {
65
62
next := NewFuture ()
66
63
go func () {
@@ -74,7 +71,8 @@ func (fut *FutureImpl) Map(f func(interface{}) (interface{}, error)) Future {
74
71
return next
75
72
}
76
73
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.
78
76
func (fut * FutureImpl ) FlatMap (f func (interface {}) (Future , error )) Future {
79
77
next := NewFuture ()
80
78
go func () {
@@ -93,35 +91,43 @@ func (fut *FutureImpl) FlatMap(f func(interface{}) (Future, error)) Future {
93
91
return next
94
92
}
95
93
96
- // Get default implementation
94
+ // Get blocks until the Future is completed and returns either a result or an error.
97
95
func (fut * FutureImpl ) Get () (interface {}, error ) {
98
96
fut .accept ()
99
97
return fut .value , fut .err
100
98
}
101
99
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.
103
102
func (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
111
113
}
112
114
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.
114
117
func (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
122
128
}
123
129
124
- // complete future with either value or error
130
+ // complete completes the Future with either a value or an error.
125
131
func (fut * FutureImpl ) complete (v interface {}, e error ) {
126
132
fut .compl .Do (func () {
127
133
go func () {
0 commit comments