promise

package module
v0.0.0-...-ac36f7e Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2022 License: MIT Imports: 3 Imported by: 0

README

Go Report Card Build Status

Introduction

lock-free / mutex-free and thread-safe promise written in Go

Supports 1.18 generics

Usage Example

Await

Await() blocks until promise resolved, or failed.

func main() {
	ip, err := getIP().Await()
	if err != nil {
		log.Printf("error: %v", err)
	} else {
		log.Printf("my ip is %s", ip)
	}
}

func getIP() *promise.Promise[string] {
	var prom promise.Promise[string]
	go func() {
		resp, err := http.Get("https://httpbin.org/ip")
		if err != nil {
			prom.Error(err)
			return
		}
		type Response struct {
			Origin string `json:"origin"`
		}
		var response Response

		err = json.NewDecoder(resp.Body).Decode(&response)
		if err != nil {
			prom.Error(err)
			return
		}
		prom.Resolve(response.Origin)
	}()
	return &prom
}

Then / Catch / Finally

func main() {
    var wg sync.WaitGroup
    wg.Add(1)
	getIP().Then(func(ip string) {
        log.Printf("my ip is %s", ip)
    }).Catch(func(err error) {
        log.Printf("error: %v", err)
    }).Finally(wg.Done)
    // wait until promise finishes
    wg.Wait()
}

Chaining multiple promises

package main

func main() {
	loc, err := getCurrentLocation().Await()
	if err != nil {
		log.Printf("error: %v", err)
	} else {
		log.Printf("im in %s, %s", loc.City, loc.Country)
	}
}

// chain multiple promise
// getIP -> getIPLocation
func getCurrentLocation() *promise.Promise[IPLocation] {
	var prom promise.Promise[IPLocation]
	getIP().Then(func(ip string) {
		// forward result
		getIPLocation(ip).
			Then(prom.Resolve).
			Catch(prom.Error)
	}).Catch(prom.Error)
	return &prom
}

func getIPLocation(ip string) *promise.Promise[IPLocation] {
	var prom promise.Promise[IPLocation]
	go func() {
		resp, err := http.Get("http://ip-api.com/json/" + ip)
		if err != nil {
			prom.Error(err)
			return
		}
		var loc IPLocation
		err = json.NewDecoder(resp.Body).Decode(&loc)
		if err != nil {
			prom.Error(err)
			return
		}
		prom.Resolve(loc)
	}()
	return &prom
}

type IPLocation struct {
	Query       string  `json:"query"`
	Status      string  `json:"status"`
	Country     string  `json:"country"`
	CountryCode string  `json:"countryCode"`
	Region      string  `json:"region"`
	RegionName  string  `json:"regionName"`
	City        string  `json:"city"`
	Zip         string  `json:"zip"`
	Lat         float64 `json:"lat"`
	Lon         float64 `json:"lon"`
	Timezone    string  `json:"timezone"`
	Isp         string  `json:"isp"`
	Org         string  `json:"org"`
	As          string  `json:"as"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pending

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

func (*Pending[T]) Resolve

func (pending *Pending[T]) Resolve(v T)

func (*Pending[T]) Then

func (pending *Pending[T]) Then(f func(T))

type PendingNoValue

type PendingNoValue struct {
	// contains filtered or unexported fields
}

func (*PendingNoValue) Resolve

func (pending *PendingNoValue) Resolve()

func (*PendingNoValue) Then

func (pending *PendingNoValue) Then(f func())

type Promise

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

func NewPromise

func NewPromise[T any]() *Promise[T]

func (*Promise[T]) Await

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

func (*Promise[T]) Catch

func (prom *Promise[T]) Catch(f func(err error)) *Promise[T]

func (*Promise[T]) Error

func (prom *Promise[T]) Error(err error)

func (*Promise[T]) Finally

func (prom *Promise[T]) Finally(f func()) *Promise[T]

func (*Promise[T]) Resolve

func (prom *Promise[T]) Resolve(v T)

func (*Promise[T]) State

func (prom *Promise[T]) State() State

func (*Promise[T]) Then

func (prom *Promise[T]) Then(f func(v T)) *Promise[T]

type State

type State int32
const (
	StatePending State = iota
	StateResolved
	StateError
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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