pinky

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 2 Imported by: 0

README

Pinky

Generic badge

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

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) Catch

func (p *Promise) Catch(f func(err error)) *Promise

Catch catches any error from promise chain

func (*Promise) CatchCase

func (p *Promise) CatchCase(targetErr error, f func(err error)) *Promise

CatchCase catches error from promise chain matches it to target error

func (*Promise) CatchDefault

func (p *Promise) CatchDefault(f func(err error)) *Promise

CatchDefault catches any error from promise chain that wasnt caught from CatchCase

func (*Promise) Delay

func (p *Promise) Delay(d time.Duration) *Promise

Delay stops promise chain

func (*Promise) Finally

func (p *Promise) Finally(f func(interface{}, error)) (interface{}, error)

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) Reject

func (p *Promise) Reject(err error)

Reject rejects promise and stores error

func (*Promise) Resolve

func (p *Promise) Resolve(res interface{})

Resolve stores value in promise

func (*Promise) Tap

func (p *Promise) Tap(f func(interface{})) *Promise

Tap allows for synchronous execution of functions chained to promise without changing promise value

func (*Promise) Then

func (p *Promise) Then(f interface{}) *Promise

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

type RejectFunc

type RejectFunc func(err error)

RejectFunc reject function

type ResolveFunc

type ResolveFunc func(res interface{})

ResolveFunc resolve function

Jump to

Keyboard shortcuts

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