promise

package
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var NotOk = errors.New("promised value is not ok")

Functions

func WrapResultFunc

func WrapResultFunc[X any](f func() (X, error)) func() P[X]

WrapResultFunc wraps an existing function "f() => (X, error)" so that it becomes "f() => P[X]", a function that returns a promise.

func WrapResultFunc1

func WrapResultFunc1[A, X any](f func(A) (X, error)) func(A) P[X]

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

func WrapResultFunc2[A, B, X any](f func(A, B) (X, error)) func(A, B) P[X]

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

func WrapResultFunc3[A, B, C, X any](f func(A, B, C) (X, error)) func(A, B, C) P[X]

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

func WrapResultFunc4[A, B, C, D, X any](f func(A, B, C, D) (X, error)) func(A, B, C, D) P[X]

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

type P[X any] interface {
	Compute() (X, error)
	ComputeCtx(ctx context.Context) (X, error)
}

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

func Chain[X any, Y any](p P[X], f func(X) (Y, error)) P[Y]

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 FromError

func FromError[X any](err error) P[X]

FromError creates a promise that simply returns the provided error when computed.

func FromFunc

func FromFunc[T any](f func() T) P[T]

FromFunc creates a promise to call function f, where f returns any single value and has no facility to indicate an error.

func FromFuncCtx

func FromFuncCtx[T any](f func(context.Context) T) P[T]

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

func FromOkFunc[X any](f func() (X, bool)) P[X]

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

func FromOkFuncCtx[X any](f func(ctx context.Context) (X, bool)) P[X]

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

func FromResultFunc[X any](f func() (X, error)) P[X]

FromResultFunc creates a promise to call function f, where f returns a (result, error) tuple.

func FromResultFuncCtx

func FromResultFuncCtx[X any](f func(context.Context) (X, error)) P[X]

FromResultFuncCtx creates a promise to call function f, where f accepts a context and returns a (result, error) tuple.

func FromValue

func FromValue[X any](x X) P[X]

FromValue creates a promise that simply returns the provided argument and a nil error when computed.

func FromValueErr

func FromValueErr[X any](value X, err error) P[X]

FromValueErr creates a promise that simply returns the provided argument or, if the provided error was non-nil, the provided error when computed.

Jump to

Keyboard shortcuts

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