-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompleted.go
46 lines (36 loc) · 1.32 KB
/
completed.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
package promise
// A completed promise. as returned by the `promise.Completed()` method.
type CompletedPromise struct {
value interface{}
}
// Create a new completed promise (with a given value). Given a value, a
// completed promise is returned, the completed promise has a `Resolved()`
// value of `true`, and `Then()` and `Combine()` execute immediately.
func Completed(value interface{}) Thenable {
completed := new(CompletedPromise)
completed.value = value
return completed
}
// Always true, a completed promise is completed from initialization.
func (promise *CompletedPromise) Resolved() bool {
return true
}
func (promise *CompletedPromise) Rejected() bool {
return false
}
// Always returns the value that this promise was initialized with.
func (promise *CompletedPromise) Get() (interface{}, error) {
return promise.value, nil
}
// Create a completed promise for the value of this promise with the compute
// function applied.
func (promise *CompletedPromise) Then(compute func(interface{}) interface{}) Thenable {
return Completed(compute(promise.value))
}
// Create a promise from this value and another promise.
func (promise *CompletedPromise) Combine(create func(interface{}) Thenable) Thenable {
return create(promise.value)
}
func (promise *CompletedPromise) Catch(handle func(error)) Thenable {
return promise
}