Documentation
¶
Overview ¶
Package pinky implements JavaScript style promises to Golang
Installation ¶
To download the the latest, run:
go get github.com/zainkai/pinky@latest
Import it in your program as:
import "github.com/zainkai/pinky"
Usage ¶
Promises can be chained together
customErr := errors.New("my promise rejected")
sum := make(chan int, 1)
go NewPromise(0).Then(func(value interface{}, resolve ResolveFunc, reject RejectFunc) {
i, _ := value.(int)
fmt.Println("Adding one.")
resolve(i + 1)
}).Then(func(value interface{}) (interface{}, error) {
i, _ := value.(int)
fmt.Println("Adding two.")
return i + 2, nil
}).Then(func(value interface{}) (interface{}, error) {
fmt.Println("Sending three to sum channel.")
i, ok := value.(int)
if !ok {
return nil, errors.New("could no type change to int")
}
return i, nil
}).CatchCase(customErr, func(err error) {
fmt.Println("err: ", err)
}).Catch(func(err error) {
fmt.Println("unknown error: ", err)
}).Finally(func(value interface{}, _ error) {
i, _ := value.(int)
sum <- i
})
fmt.Println("Recieved Value: ", <-sum)
Promises Also have integrated channels
adderPromise := NewPromise(10)
sum := adderPromise.GetChan()
go adderPromise.Then(func(value interface{}) (interface{}, error) {
i, _ := value.(int)
fmt.Println("Adding one.")
return i + 1, nil
}).Finally(func(value interface{}, _ error) {
i, _ := value.(int)
fmt.Println("final promise value: ", i)
// the PromiseResult will be sent after final is called.
})
fmt.Println("Recieved Value: ", <-sum)
Index ¶
- type Promise
- func (p *Promise) Catch(f func(err error)) *Promise
- func (p *Promise) CatchCase(targetErr error, f func(err error)) *Promise
- func (p *Promise) CatchDefault(f func(err error)) *Promise
- func (p *Promise) Delay(d time.Duration) *Promise
- func (p *Promise) Finally(f func(interface{}, error)) (interface{}, error)
- func (p *Promise) GetChan() chan PromiseResult
- func (p *Promise) Reject(err error)
- func (p *Promise) Resolve(res interface{})
- func (p *Promise) Tap(f func(interface{})) *Promise
- func (p *Promise) Then(f interface{}) *Promise
- type PromiseResult
- type RejectFunc
- type ResolveFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Promise ¶
type Promise struct {
// contains filtered or unexported fields
}
Promise ...
func NewPromise ¶
func NewPromise(value interface{}) *Promise
NewPromise Creates a new promise with a starting value
func (*Promise) CatchDefault ¶
CatchDefault catches any error from promise chain that wasnt caught from CatchCase
func (*Promise) Finally ¶
Finally allows the promise to finish. This chain can be used to signal go channels for async execution.
func (*Promise) GetChan ¶ added in v1.0.1
func (p *Promise) GetChan() chan PromiseResult
GetChan returns a channel with PromiseResult. The result is only send when `.Finally` is called.
func (*Promise) Resolve ¶
func (p *Promise) Resolve(res interface{})
Resolve stores value in promise
func (*Promise) Tap ¶
Tap allows for synchronous execution of functions chained to promise without changing promise value
func (*Promise) Then ¶
Then allows for synchronous execution of functions chained to promise
// Param f has multi types types:
// func(value interface{}, resolve ResolveFunc, reject RejectFunc)
// func(resolve ResolveFunc, reject RejectFunc)
// func(value interface{}) (interface{}, error)
// func() (interface{}, error)
// func(value interface{}) error
// func() error
// func(value interface{})
// func()
type PromiseResult ¶ added in v1.0.1
type PromiseResult struct {
Value interface{}
Err error
}
PromiseResult returns the value and or error of a promise