promise

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error[T any] struct{ Err error }

Error is the Result of a failed process.

func ErrorOf

func ErrorOf[T any](err error) Error[T]

ErrorOf returns an Error for the given error and result type.

func (Error[T]) Get

func (e Error[T]) Get() (T, error)

Get returns the error.

type Panic

type Panic[T any] struct{ Value any }

Panic is the Result of a process that panicked.

func (Panic[T]) Get

func (p Panic[T]) Get() (T, error)

Get returns an error with the panic details.

type Promise

type Promise[T any] interface {
	// Result returns the result of the promise.
	Result() Result[T]
}

A Promise represents an asynchronous process.

func Call

func Call[T any](fn func() Result[T]) Promise[T]

Call spawns a concurrent process that calls the function.

func Catch

func Catch[T any](promise Promise[T], fn func(error) Result[T]) Promise[T]

Catch spawns a concurrent process that waits for the promise to resolve and calls the function if the promise fails, passing the result to the function.

func New

func New[T any]() (_ Promise[T], resolve func(T), reject func(error))

New returns a Promise that behaves more like traditional promises, returning resolve and reject callbacks.

func Rejected

func Rejected[T any](err error) Promise[T]

Rejected returns a promise that resolves to an Error.

func Resolved

func Resolved[T any](v T) Promise[T]

Resolved returns a promise that resolves to a Value.

func SyncCall

func SyncCall[T any](mu sync.Locker, fn func() Result[T]) Promise[T]

SyncCall spawns a process that acquires the lock, calls the function, and releases the lock.

func SyncThen

func SyncThen[T, S any](mu sync.Locker, promise Promise[T], fn func(T) Result[S]) Promise[S]

SyncThen spawns a process that waits for the promise to resolve; if the promise is successful, the process acquires the lock, calls the function, and releases the lock.

func Then

func Then[T, S any](promise Promise[T], fn func(T) Result[S]) Promise[S]

Then spawns a concurrent process that waits for the promise to resolve and calls the function if the promise succeeds, passing the result to the function.

type Result

type Result[T any] interface {
	// Get returns the resultant value or an error.
	Get() (T, error)
}

Result is the result of a promise.

func ValueOrError

func ValueOrError[T any](v T, err error) Result[T]

ValueOrError returns a Value if the error is nil, otherwise returning an Error.

type ResultPromise

type ResultPromise[T any] struct{ Promise Promise[T] }

ResultPromise is a Promise that satisfies Result.

func (ResultPromise[T]) Get

func (v ResultPromise[T]) Get() (T, error)

Get returns the result of the promise.

type Value

type Value[T any] struct{ Value T }

Value is the Result of a successful process.

func ValueOf

func ValueOf[T any](v T) Value[T]

ValueOf returns a Value for the given value.

func (Value[T]) Get

func (v Value[T]) Get() (T, error)

Get returns the value.

Jump to

Keyboard shortcuts

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