Help us shape the future of Go! Take the 2026 Go Developer Survey

promise

package
v1.34.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 6 Imported by: 2

Documentation

Index

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 Once added in v1.26.0

type Once[T comparable] struct {
	// contains filtered or unexported fields
}

Once contains a function that is called concurrently once.

The result is returned as a promise. If the function returns no error, the result is stored and memoized.

Otherwise, future calls to the function will try again.

func NewOnce added in v1.26.0

func NewOnce[T comparable](cb func(ctx context.Context) (T, error)) *Once[T]

NewOnce constructs a new Once caller.

func (*Once[T]) Resolve added in v1.26.0

func (o *Once[T]) Resolve(ctx context.Context) (T, error)

Resolve attempts to resolve the value using the ctx.

type Promise

type Promise[T any] struct {
	// contains filtered or unexported fields
}

Promise is an asynchronous result to an operation.

func NewPromise

func NewPromise[T any]() *Promise[T]

NewPromise constructs a new empty Promise.

func NewPromiseWithErr

func NewPromiseWithErr[T any](err error) *Promise[T]

NewPromiseWithErr constructs a promise pre-resolved with an error.

func NewPromiseWithResult

func NewPromiseWithResult[T any](val T, err error) *Promise[T]

NewPromiseWithResult constructs a promise pre-resolved with a result.

func (*Promise[T]) Await

func (p *Promise[T]) Await(ctx context.Context) (val T, err error)

Await waits for the result to be set or for ctx to be canceled.

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

func (p *Promise[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 (*Promise[T]) SetResult

func (p *Promise[T]) SetResult(val T, err error) bool

SetResult sets the result of the promise.

Returns false if the result was already set.

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() (prom PromiseLike[T], waitCh <-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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL