-
Notifications
You must be signed in to change notification settings - Fork 0
/
rejected.go
84 lines (67 loc) · 2.18 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
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
/**********************************************************\
* *
* promise/rejected.go *
* *
* rejected promise implementation for Go. *
* *
* LastModified: Aug 18, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
package promise
import "time"
type rejected struct {
reason error
}
// Reject creates a REJECTED Promise object
func Reject(reason error) Promise {
return &rejected{reason}
}
func (p *rejected) Then(onFulfilled OnFulfilled, onRejected ...OnRejected) Promise {
if len(onRejected) == 0 {
return &rejected{p.reason}
}
next := New()
reject(next, onRejected[0], p.reason)
return next
}
func (p *rejected) Catch(onRejected OnRejected, test ...func(error) bool) Promise {
if len(test) == 0 || test[0](p.reason) {
next := New()
reject(next, onRejected, p.reason)
return next
}
return &rejected{p.reason}
}
func (p *rejected) Complete(onCompleted OnCompleted) Promise {
return p.Then(nil, onCompleted)
}
func (p *rejected) WhenComplete(action func()) Promise {
return p.Catch(func(e error) (interface{}, error) {
action()
return nil, e
})
}
func (p *rejected) Done(onFulfilled OnFulfilled, onRejected ...OnRejected) {
p.Then(nil, onRejected...).Then(nil, func(e error) { go panic(e) })
}
func (p *rejected) State() State {
return REJECTED
}
func (p *rejected) Resolve(value interface{}) {}
func (p *rejected) Reject(reason error) {}
func (p *rejected) Fill(promise Promise) {
go promise.Reject(p.reason)
}
func (p *rejected) Timeout(duration time.Duration, reason ...error) Promise {
return timeout(p, duration, reason...)
}
func (p *rejected) Delay(duration time.Duration) Promise {
return &rejected{p.reason}
}
func (p *rejected) Tap(onfulfilledSideEffect func(interface{})) Promise {
return &rejected{p.reason}
}
func (p *rejected) Get() (interface{}, error) {
return nil, p.reason
}