-
Notifications
You must be signed in to change notification settings - Fork 5
/
rejected.go
47 lines (35 loc) · 1.08 KB
/
rejected.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
package promise
type RejectedPromise struct {
cause error
}
// Create a new pure promise which has already been rejected. All calls to
// Then() and Combine() simply return this promise without executing any
// further code, and calls to Catch() invoke the handler immediately with the
// suppilied cause. Not supplying a cause is considered an illegal state.
func Rejected(cause error) Thenable {
if cause == nil {
panic("Rejected promise requires a non-nil cause")
}
promise := new(RejectedPromise)
promise.cause = cause
return promise
}
func (promise *RejectedPromise) Resolved() bool {
return false
}
func (promise *RejectedPromise) Rejected() bool {
return true
}
func (promise *RejectedPromise) Get() (interface{}, error) {
return nil, promise.cause
}
func (promise *RejectedPromise) Then(compute func(interface{}) interface{}) Thenable {
return promise
}
func (promise *RejectedPromise) Combine(compute func(interface{}) Thenable) Thenable {
return promise
}
func (promise *RejectedPromise) Catch(handle func(error)) Thenable {
handle(promise.cause)
return promise
}