promise

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoPromises = errors.New("no promises specified")

NoPromises is used for Race where it is expected that promises will be set.

Functions

func All

func All[T any](promises ...*Promise[T]) ([]T, error)

All is used to return all results when all promises have resolved. If one promise rejects, the error will be returned.

func Iterator

func Iterator[T any](promises ...*Promise[T]) func() (val T, end bool, err error)

Iterator is used to create a function to iterate over promises. Next will block until the next promise resolves. Note the next function is not thread safe!

func Race

func Race[T any](promises ...*Promise[T]) (T, error)

Race returns the result of the first promise to resolve.

Types

type Promise

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

Promise is a promise that can be resolved or rejected. Note that manually creating this will result in blank values. You probably want to use .NewRejected, .NewResolved, or .NewFn instead.

func Catch

func Catch[T any, X any](p *Promise[T], f func(error) (X, error)) *Promise[X]

Catch is used to add a error catching handler to the promise. In the event that the promise has already resolved, this will result in a new go-routine being spawned.

func NewFn

func NewFn[T any](f func() (T, error)) *Promise[T]

NewFn is used to create a new function promise.

func NewRejected

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

NewRejected is used to create a new rejected promise.

func NewResolved

func NewResolved[T any](result T) *Promise[T]

NewResolved is used to create a new resolved promise.

func Then

func Then[T any, X any](p *Promise[T], f func(T) (X, error)) *Promise[X]

Then is used to add a then handler to the promise. In the event that the promise has already resolved, this will result in a new go-routine being spawned.

func (*Promise[T]) Resolve

func (p *Promise[T]) Resolve() *PromiseResolution[T]

Resolve is used to get the promise resolution. Returns a nil pointer if the promise is unresolved.

type PromiseResolution

type PromiseResolution[T any] struct {
	// Result defines the result of the promise.
	Result T

	// Error defines if the promise rejected.
	Error error
}

PromiseResolution is used to define the resolution of a promise.

Jump to

Keyboard shortcuts

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