promise

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT Imports: 2 Imported by: 0

README

gopromise

Like Node.JS Promises, but in Go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeferredPromise added in v1.1.0

type DeferredPromise[T any] interface {
	Promise[T]

	// Run causes the DeferredPromise to call the passed Func,
	// thus starting the Promise lifecycle.
	// Run is multiprocess-safe: after the first call, subsequent calls
	// will do nothing.
	Run(Func[T])

	// Started returns whether or not Run has been called for
	// this DeferredPromise.
	Started() bool
}

DeferredPromise is a Promise which isn't initialized with a Func; instead, a DeferredPromise begins its lifecycle when Run is called.

func NewDeferredPromise added in v1.1.0

func NewDeferredPromise[T any]() DeferredPromise[T]

NewDeferredPromise returns a DeferredPromise.

type Func

type Func[T any] func(ResolveFunc[T], RejectFunc)

Func represents a wrapper function, to be wrapped around functions which are being called by a Promise.

type Promise

type Promise[T any] interface {
	// Resolve blocks until this Promise's execution finishes and
	// a Result is returned.
	Resolve() Result[T]

	// ResolveAsync returns a chan(Result) with a buffer size of 1, which
	// can be utilized in any way desired.
	ResolveAsync() <-chan Result[T]

	// Fulfilled returns true if this Promise resolved successfully.
	// More specifically, it returns true if the result of Err is nil;
	// the value returned by Result _can_ still be nil even if Fulfilled returns true.
	Fulfilled() bool

	// Rejected returns true if this Promise did not resolve successfully - specifically,
	// if the return result of Err is not nil.
	// Rejected will return false if the result of both Err and Result are nil.
	Rejected() bool
}

Promise provides an interface for types which fetch data or calculate results asyncronously, and can then wait for the result to return.

func NewPromise

func NewPromise[T any](fn Func[T]) Promise[T]

NewPromise returns a Promise. Upon instantiation, the passed Func is called in a goroutine which stores the returned data/error.

type RejectFunc added in v1.3.0

type RejectFunc func(error)

RejectFunc is a function passed to functions called by Promise, to be called if they return an error.

type ResolveFunc added in v1.3.0

type ResolveFunc[T any] func(T)

ResolveFunc is a function passed to functions called by Promise, to be called by those functions if they successfully return a result.

type Result

type Result[T any] interface {
	// Result returns the result of a Promise function call,
	// or nil if Err != nil
	Result() *T

	// Err returns any resultant error of a Promise function call.
	Err() error
}

Result is the result data returned by a function called by a Promise.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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