-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.go
179 lines (160 loc) · 6.24 KB
/
promise.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**********************************************************\
* *
* promise/promise.go *
* *
* promise interface for Go. *
* *
* LastModified: Sep 11, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
package promise
import "time"
// OnFulfilled is a function called when the Promise is fulfilled.
//
// This function has one argument, the fulfillment value.
//
// The function type can be the following form:
//
// func()
// func() R
// func() (R, error)
// func(T)
// func(T) R
// func(T) (R, error)
//
// T and R can be any type, and they are can be the same or different.
type OnFulfilled interface{}
// OnRejected is a function called when the Promise is rejected.
//
// This function has one argument, the rejection reason.
//
// The function type can be the following form:
//
// func()
// func() T
// func() (T, error)
// func(E)
// func(E) T
// func(E) (T, error)
//
// E is error type or interface{}.
// T can be any type.
type OnRejected interface{}
// OnCompleted is a function called when the Promise is completed.
//
// This function has one argument, the fulfillment value when the Promise is
// fulfilled, or the rejection reason when the Promise is rejected.
//
// The function type can be the following form:
//
// func()
// func() T
// func() (T, error)
// func(interface{})
// func(interface{}) T
// func(interface{}) (T, error)
//
// T can be any type.
type OnCompleted interface{}
// Computation is a function for Promise Create.
//
// This function has no argument.
//
// The function type can be the following form:
//
// func()
// func() T
// func() (T, error)
//
// T can be any type.
type Computation interface{}
// Promise is an interface of the JS Promise/A+ spec
// (https://promisesaplus.com/).
type Promise interface {
// Then method returns a Promise. It takes two arguments: callback functions
// for the success and failure cases of the Promise.
Then(onFulfilled OnFulfilled, onRejected ...OnRejected) Promise
// Catch handles errors emitted by this Promise.
//
// This is the asynchronous equivalent of a "catch" block.
//
// Returns a new Promise that will be completed with either the result of
// this promise or the result of calling the onRejected callback.
//
// If this promise completes with a value, the returned promise completes
// with the same value.
//
// If this promise completes with an error, then test is first called with
// the error value.
//
// If test returns false, the error is not handled by this Catch, and the
// returned promise completes with the same error and stack trace as this
// promise.
//
// If test returns true, onRejected is called with the error and possibly
// stack trace, and the returned promise is completed with the result of
// this call in exactly the same way as for Then's onRejected.
//
// If test is omitted, it defaults to a function that always returns true.
// The test function should not panic, but if it does, it is handled as if
// the the onRejected function had panic.
Catch(onRejected OnRejected, test ...func(error) bool) Promise
// Complete is the same way as Then(onCompleted, onCompleted)
Complete(onCompleted OnCompleted) Promise
// WhenComplete register a function to be called when the promise completes.
//
// The action function is called when this promise completes, whether it
// does so with a value or with an error.
//
// If this promise completes with a value, the returned promise completes
// with the same value.
//
// If this promise completes with an error, the returned promise completes
// with the same error.
//
// The action function should not panic, but if it does, the returned
// promise completes with a PanicError.
WhenComplete(action func()) Promise
// Done is the same semantics as Then except that it don't return a Promise.
// If the callback function (onFulfilled or onRejected) returns error or
// panics, the application will be crashing.
// The result of the callback function will be ignored.
Done(onFulfilled OnFulfilled, onRejected ...OnRejected)
// State return the current state of the Promise
State() State
// Resolve method returns a Promise object that is resolved with the given
// value. If the value is a Promise, the returned promise will "follow"
// that Promise, adopting its eventual state; otherwise the returned promise
// will be fulfilled with the value.
Resolve(value interface{})
// Reject method returns a Promise object that is rejected with the given
// reason.
Reject(reason error)
// Fill the promise with this promise if the promise is in PENDING state.
// otherwise nothing to do.
Fill(promise Promise)
// Timeout create a new promise that will reject with a TimeoutError or a
// custom reason after a timeout if promise does not fulfill or reject
// beforehand.
Timeout(duration time.Duration, reason ...error) Promise
// Delay create a new promise that will, after duration delay, fulfill with
// the same value as this promise. If this promise rejects, delayed promise
// will be rejected immediately.
Delay(duration time.Duration) Promise
// Tap executes a function as a side effect when promise fulfills.
//
// It returns a new promise:
// 1. If promise fulfills, onFulfilledSideEffect is executed:
// * If onFulfilledSideEffect returns successfully, the promise
// returned by tap fulfills with promise's original fulfillment
// value.
// * If onFulfilledSideEffect panics, the promise returned by tap
// rejects with the panic message as the reason.
// 2. If promise rejects, onFulfilledSideEffect is not executed, and the
// promise returned by tap rejects with promise's rejection reason.
Tap(onfulfilledSideEffect func(interface{})) Promise
// Get the value and reason synchronously, if this promise in PENDING state.
// this method will block the current goroutine.
Get() (interface{}, error)
}