promise

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2022 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inspect

func Inspect(typ reflect.Type) (reflect.Type, bool)

Types

type Deferred

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

Deferred represents the computation that fulfills a Promise.

func Defer

func Defer[T any]() Deferred[T]

Defer creates a Deferred computation.

func (Deferred[T]) Promise

func (d Deferred[T]) Promise() *Promise[T]

func (Deferred[T]) Reject

func (d Deferred[T]) Reject(err error)

func (Deferred[T]) Resolve

func (d Deferred[T]) Resolve(resolution T)

type Promise

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

Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

func All

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

All resolves when all the input's promises have resolved. All rejects immediately upon any of the input promises rejecting. All returns nil if the input is empty.

func Any

func Any[T any](promises ...*Promise[T]) *Promise[T]

Any resolves as soon as any of the input's Promises resolve, with the value of the resolved Promise. Any rejects if all the given Promises are rejected with a combination of all errors. Any returns nil if the input is empty.

func Catch

func Catch[T any](promise *Promise[T], rejection func(err error) error) *Promise[T]

Catch allows to chain promises. Use it to add an error handler to the rejected promise.

func Coerce

func Coerce[T any](promise *Promise[any]) *Promise[T]

func Delay

func Delay(delay time.Duration) *Promise[Void]

func New

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

New creates a new Promise

func Race

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

Race resolves or rejects as soon as one of the input's Promises resolve or reject, with the value or error of that Promise. Race returns nil if the input is empty.

func Reject

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

Reject returns a Promise that has been rejected with a given error.

func Resolve

func Resolve[T any](resolution T) *Promise[T]

Resolve returns a Promise that has been resolved with a given value.

func Then

func Then[A, B any](promise *Promise[A], resolveA func(data A) B) *Promise[B]

Then allows to chain promises. Use it to add a handler to the resolved promise.

func Unwrap

func Unwrap[T any](promise *Promise[*Promise[T]]) *Promise[T]

func (*Promise[T]) Await

func (p *Promise[T]) Await() (T, error)

Await blocks until the promise is resolved or rejected.

func (*Promise[T]) AwaitAny

func (p *Promise[T]) AwaitAny() (any, error)

func (*Promise[T]) Catch

func (p *Promise[T]) Catch(rej func(err error) error) *Promise[any]

func (*Promise[T]) Coerce

func (p *Promise[T]) Coerce(promise *Promise[any])

func (*Promise[T]) Lift

func (p *Promise[T]) Lift(result any)

func (*Promise[T]) Then

func (p *Promise[T]) Then(res func(data any) any) *Promise[any]

func (*Promise[T]) UnderlyingType

func (p *Promise[T]) UnderlyingType() reflect.Type

type Reflect

type Reflect interface {
	UnderlyingType() reflect.Type

	Then(resolve func(data any) any) *Promise[any]
	Catch(reject func(err error) error) *Promise[any]
	AwaitAny() (any, error)

	Lift(result any)
	Coerce(promise *Promise[any])
}

func CoerceType

func CoerceType(typ reflect.Type, promise *Promise[any]) Reflect

func Lift

func Lift(typ reflect.Type, result any) Reflect

type Void

type Void = struct{}

Jump to

Keyboard shortcuts

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