Documentation
¶
Overview ¶
Package promise implements a simple Promise type that can be used to represent a computation to be performed at a later stage.
This composes nicely with the idea of futures (see fun/future).
Tip: rarely return a Promise from any function. Instead, return a normal (result, error) tuple. The caller can use WrapResultFunc to transform that function into one that returns a promise.
Index ¶
- Variables
- func WrapResultFunc[X any](f func() (X, error)) func() P[X]
- func WrapResultFunc1[A, X any](f func(A) (X, error)) func(A) P[X]
- func WrapResultFunc2[A, B, X any](f func(A, B) (X, error)) func(A, B) P[X]
- func WrapResultFunc3[A, B, C, X any](f func(A, B, C) (X, error)) func(A, B, C) P[X]
- func WrapResultFunc4[A, B, C, D, X any](f func(A, B, C, D) (X, error)) func(A, B, C, D) P[X]
- type P
- func Chain[X any, Y any](p P[X], f func(X) (Y, error)) P[Y]
- func FromError[X any](err error) P[X]
- func FromFunc[T any](f func() T) P[T]
- func FromFuncCtx[T any](f func(context.Context) T) P[T]
- func FromOkFunc[X any](f func() (X, bool)) P[X]
- func FromOkFuncCtx[X any](f func(ctx context.Context) (X, bool)) P[X]
- func FromResultFunc[X any](f func() (X, error)) P[X]
- func FromResultFuncCtx[X any](f func(context.Context) (X, error)) P[X]
- func FromValue[X any](x X) P[X]
- func FromValueErr[X any](value X, err error) P[X]
Constants ¶
This section is empty.
Variables ¶
var NotOk = errors.New("promised value is not ok")
Functions ¶
func WrapResultFunc ¶
WrapResultFunc wraps an existing function "f() => (X, error)" so that it becomes "f() => P[X]", a function that returns a promise.
func WrapResultFunc1 ¶
WrapResultFunc1 wraps an existing function "f(A) => (X, error)" so that it becomes "f(A) => P[X]", a function that returns a promise.
func WrapResultFunc2 ¶
WrapResultFunc2 wraps an existing function "f(A, B) => (X, error)" so that it becomes "f(A, B) => P[X]", a function that returns a promise.
func WrapResultFunc3 ¶
WrapResultFunc3 wraps an existing function "f(A, B, C) => (X, error)" so that it becomes "f(A, B, C) => P[X]", a function that returns a promise.
func WrapResultFunc4 ¶
WrapResultFunc4 wraps an existing function "f(A, B, C, D) => (X, error)" so that it becomes "f(A, B, C, D) => P[X]", a function that returns a promise.
Types ¶
type P ¶
P represents a promise to calculate and return some value when Compute or ComputeCtx is called.
Compute, or ComputeCtx, should only be called once, unless an implementation otherwise indicates that it is safe to do so. A promise is not safe for concurrent use, unless an implementation indicates otherwise.
A promise may ignore the provided context if it cannot be cancelled. The plain Compute method computes the promise with a context that is never cancelled.
The error return value of Compute and ComputeCtx may be an error returned by the computation, or, in the case of ComputeCtx, a context error such as context.Cancelled.
func Chain ¶
Chain returns a new promise to compute function f on the result of promise p. If the input promise returns an error, the returned promise will return that error without calling f.
func FromFunc ¶
FromFunc creates a promise to call function f, where f returns any single value and has no facility to indicate an error.
func FromFuncCtx ¶
FromFuncCtx creates a promise to call function f, where f accepts a context and returns any single value and has no facility to indicate an error, other than a context error.
func FromOkFunc ¶
FromOkFunc creates a promise to call function f, where f returns a (value, ok) tuple. If the returned ok is false, the promise computes the error NotOk.
func FromOkFuncCtx ¶
FromOkFuncCtx creates a promise to call function f, where f accepts a context and returns a (value, ok) tuple. If the returned ok is false, the promise computes the error NotOk.
func FromResultFunc ¶
FromResultFunc creates a promise to call function f, where f returns a (result, error) tuple.
func FromResultFuncCtx ¶
FromResultFuncCtx creates a promise to call function f, where f accepts a context and returns a (result, error) tuple.
func FromValue ¶
FromValue creates a promise that simply returns the provided argument and a nil error when computed.
func FromValueErr ¶
FromValueErr creates a promise that simply returns the provided argument or, if the provided error was non-nil, the provided error when computed.