-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeferred.go
99 lines (90 loc) · 2.61 KB
/
deferred.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
package vgo
type Deferred struct {
Callback func() interface{}
}
type ResultDeferred struct {
result chan interface{}
}
/**
* Deferred creates a new Deferred instance and runs the callback function in a new goroutine.
* @return *Derrered The Deferred instance.
*/
func (p *Deferred) Run() *ResultDeferred {
result := make(chan interface{})
go func() {
result <- p.Callback()
}()
return &ResultDeferred{result}
}
/**
* Await waits for the Deferred instance to finish and returns the result.
* @return interface{} The result of the Deferred instance.
*/
func (p *Deferred) Await() interface{} {
return p.Run().Await()
}
/**
* Await waits for the Deferred instance to finish and returns the result.
* @return interface{} The result of the Deferred instance.
* @deprecated Use Run().Await() instead.
*/
func (p *ResultDeferred) Await() interface{} {
return <-p.result
}
/**
* Then waits for the Deferred instance to finish and returns the result.
* @param callbacks The callback functions to run after the Deferred instance is finished.
* @return interface{} The result of the Deferred instance.
*/
func (p *Deferred) All(callbacks ...func() interface{}) *ResultDeferred {
result := make(chan interface{})
go func() {
var results []interface{}
for _, callback := range callbacks {
results = append(results, callback())
}
result <- results
}()
return &ResultDeferred{result}
}
/**
* Any waits for the Deferred instance to finish and returns the result.
* @param callbacks The callback functions to run after the Deferred instance is finished.
* @return interface{} The result of the Deferred instance.
*/
func (p *Deferred) Any(callbacks ...func() interface{}) *ResultDeferred {
result := make(chan interface{})
go func() {
for _, callback := range callbacks {
if callback() != nil {
result <- callback()
return
}
}
result <- nil
}()
return &ResultDeferred{result}
}
/**
* Race waits for the Deferred instance to finish and returns the result.
* @param callbacks The callback functions to run after the Deferred instance is finished.
* @return interface{} The result of the Deferred instance.
*/
func (p *Deferred) Race(callbacks ...func() interface{}) *ResultDeferred {
result := make(chan interface{})
go func() {
for _, callback := range callbacks {
result <- callback()
return
}
}()
return &ResultDeferred{result}
}
/**
* Other method to create a new Deferred instance.
* @param callback The callback function to run in a new goroutine.
* @return *Deferred The Deferred instance.
*/
func NewDeferred(callback func() interface{}) *Deferred {
return &Deferred{callback}
}