-
Notifications
You must be signed in to change notification settings - Fork 11
/
promise.go
53 lines (42 loc) · 1.14 KB
/
promise.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
package async
import "sync"
// Promise represents a writable, single-assignment container,
// which completes a Future.
type Promise[T any] interface {
// Success completes the underlying Future with a value.
Success(T)
// Failure fails the underlying Future with an error.
Failure(error)
// Future returns the underlying Future.
Future() Future[T]
}
// promiseImpl implements the Promise interface.
type promiseImpl[T any] struct {
once sync.Once
future Future[T]
}
// Verify promiseImpl satisfies the Promise interface.
var _ Promise[any] = (*promiseImpl[any])(nil)
// NewPromise returns a new Promise.
func NewPromise[T any]() Promise[T] {
return &promiseImpl[T]{
future: newFuture[T](),
}
}
// Success completes the underlying Future with a given value.
func (p *promiseImpl[T]) Success(value T) {
p.once.Do(func() {
p.future.complete(value, nil)
})
}
// Failure fails the underlying Future with a given error.
func (p *promiseImpl[T]) Failure(err error) {
p.once.Do(func() {
var zero T
p.future.complete(zero, err)
})
}
// Future returns the underlying Future.
func (p *promiseImpl[T]) Future() Future[T] {
return p.future
}