promise

package
v0.0.0-...-51f9457 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoData is an error returned when a request completes without available data.
	ErrNoData = errors.New("promise: No Data")
)

Functions

This section is empty.

Types

type Generator

type Generator func(context.Context) (interface{}, error)

Generator is the Promise's generator type.

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a map from some key to a promise that does something associated with this key.

First call to Get initiates a new promise. All subsequent calls return exact same promise (even if it has finished).

func (*Map) Get

func (pm *Map) Get(c context.Context, key interface{}, gen Generator) *Promise

Get either returns an existing promise for the given key or creates and immediately launches a new promise.

type Promise

type Promise struct {
	// contains filtered or unexported fields
}

Promise is a promise structure with goroutine-safe methods that is responsible for owning a single piece of data. Promises have multiple readers and a single writer.

Readers will retrieve the Promise's data via Get(). If the data has not been populated, the reader will block pending the data. Once the data has been delivered, all readers will unblock and receive a reference to the Promise's data.

func New

func New(c context.Context, gen Generator) *Promise

New instantiates a new, empty Promise instance. The Promise's value will be the value returned by the supplied generator function.

The generator will be invoked immediately in its own goroutine.

func NewDeferred

func NewDeferred(gen Generator) *Promise

NewDeferred instantiates a new, empty Promise instance. The Promise's value will be the value returned by the supplied generator function.

Unlike New, the generator function will not be immediately executed. Instead, it will be run when the first call to Get is made, and will use one of the Get callers' goroutines. goroutine a Get caller.

func (*Promise) Get

func (p *Promise) Get(c context.Context) (interface{}, error)

Get returns the promise's value. If the value isn't set, Get will block until the value is available, following the Context's timeout parameters.

If the value is available, it will be returned with its error status. If the context times out or is cancelled, the appropriate context error will be returned.

func (*Promise) Peek

func (p *Promise) Peek() (interface{}, error)

Peek returns the promise's current value. If the value isn't set, Peek will return immediately with ErrNoData.

Jump to

Keyboard shortcuts

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