Documentation ¶
Index ¶
- func CheckPromiseLike(ctx context.Context, ctor func() PromiseLike[int]) error
- type Promise
- func (p *Promise[T]) Await(ctx context.Context) (val T, err error)
- func (p *Promise[T]) AwaitWithCancelCh(ctx context.Context, cancelCh <-chan struct{}) (val T, err error)
- func (p *Promise[T]) AwaitWithErrCh(ctx context.Context, errCh <-chan error) (val T, err error)
- func (p *Promise[T]) SetResult(val T, err error) bool
- type PromiseContainer
- func (p *PromiseContainer[T]) Await(ctx context.Context) (val T, err error)
- func (p *PromiseContainer[T]) AwaitWithCancelCh(ctx context.Context, cancelCh <-chan struct{}) (val T, err error)
- func (p *PromiseContainer[T]) AwaitWithErrCh(ctx context.Context, errCh <-chan error) (val T, err error)
- func (c *PromiseContainer[T]) GetPromise() (PromiseLike[T], <-chan struct{})
- func (c *PromiseContainer[T]) SetPromise(p PromiseLike[T])
- func (p *PromiseContainer[T]) SetResult(val T, err error) bool
- type PromiseLike
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckPromiseLike ¶
func CheckPromiseLike(ctx context.Context, ctor func() PromiseLike[int]) error
CheckPromiseLike runs some tests against the PromiseLike.
intended to be used in go tests
Types ¶
type Promise ¶
type Promise[T any] struct { // contains filtered or unexported fields }
Promise is an asynchronous result to an operation.
func NewPromiseWithErr ¶
NewPromiseWithErr constructs a promise pre-resolved with an error.
func NewPromiseWithResult ¶
NewPromiseWithResult constructs a promise pre-resolved with a result.
func (*Promise[T]) AwaitWithCancelCh ¶
func (p *Promise[T]) AwaitWithCancelCh(ctx context.Context, cancelCh <-chan struct{}) (val T, err error)
AwaitWithCancelCh waits for the result to be set or for the channel to be written to and/or closed. Returns nil, context.Canceled if the cancelCh reads.
func (*Promise[T]) AwaitWithErrCh ¶
AwaitWithErrCh waits for the result to be set or for an error to be pushed to the channel.
type PromiseContainer ¶
type PromiseContainer[T any] struct { // contains filtered or unexported fields }
PromiseContainer contains a Promise which can be replaced with a new Promise.
The zero-value of this struct is valid.
func NewPromiseContainer ¶
func NewPromiseContainer[T any]() *PromiseContainer[T]
NewPromiseContainer constructs a new PromiseContainer.
func (*PromiseContainer[T]) Await ¶
func (p *PromiseContainer[T]) Await(ctx context.Context) (val T, err error)
Await waits for the result to be set or for ctx to be canceled.
func (*PromiseContainer[T]) AwaitWithCancelCh ¶
func (p *PromiseContainer[T]) AwaitWithCancelCh(ctx context.Context, cancelCh <-chan struct{}) (val T, err error)
AwaitWithCancelCh waits for the result to be set or for the channel to be written to and/or closed.
CancelCh could be a context.Done() channel.
Will return nil, nil if the cancelCh is closed. Returns nil, context.Canceled if ctx is canceled. Otherwise waits for a value or an error to be set to the promise.
func (*PromiseContainer[T]) AwaitWithErrCh ¶
func (p *PromiseContainer[T]) AwaitWithErrCh(ctx context.Context, errCh <-chan error) (val T, err error)
AwaitWithErrCh waits for the result to be set or for an error to be pushed to the channel.
func (*PromiseContainer[T]) GetPromise ¶ added in v1.1.2
func (c *PromiseContainer[T]) GetPromise() (PromiseLike[T], <-chan struct{})
GetPromise returns the Promise contained in the PromiseContainer and a channel that is closed when the Promise is replaced.
Note that promise may be nil.
func (*PromiseContainer[T]) SetPromise ¶
func (c *PromiseContainer[T]) SetPromise(p PromiseLike[T])
SetPromise updates the Promise contained in the PromiseContainer. Note: this does not do anything with the old promise.
func (*PromiseContainer[T]) SetResult ¶
func (p *PromiseContainer[T]) SetResult(val T, err error) bool
SetResult sets the result of the promise.
Overwrites the existing promise with a new promise.
type PromiseLike ¶
type PromiseLike[T any] interface { // SetResult sets the result of the promise. // // Returns false if the result was already set. SetResult(val T, err error) bool // Await awaits for a result or for ctx to be canceled. Await(ctx context.Context) (val T, err error) // AwaitWithErrCh waits for the result to be set or for an error to be pushed to the channel. AwaitWithErrCh(ctx context.Context, errCh <-chan error) (val T, err error) // AwaitWithCancelCh waits for the result to be set or for the channel to be written to and/or closed. // CancelCh could be a context.Done() channel. AwaitWithCancelCh(ctx context.Context, errCh <-chan struct{}) (val T, err error) }
PromiseLike is any object which satisfies the Promise interface.