-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.go
More file actions
34 lines (31 loc) · 912 Bytes
/
async.go
File metadata and controls
34 lines (31 loc) · 912 Bytes
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
package vgo
type Async struct {
Callback func() interface{}
}
/**
* Async creates a new Deferred instance and runs the callback function in a new goroutine.
* @return *Derrered The Deferred instance.
*/
func (p *Async) Run() *Deferred {
return &Deferred{
Callback: p.Callback,
}
}
/**
* Await waits for the Deferred instance to finish and returns the result.
* @param value The Deferred instance to wait for.
* @return interface{} The result of the Deferred instance.
*/
func Await(value interface{}) interface{} {
switch v := value.(type) {
case *Async:
return v.Run().Run().Await()
case *Deferred:
return v.Run().Await()
case func() interface{}:
// Create a new Async instance and run the callback function.
return (&Async{Callback: v}).Run().Run().Await()
default:
panic("Await expects an Async function, callback or a Deferred instance!")
}
}