promise

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0

README

promises GoDoc

Promises is a library that builds something similar to JS style promises, or Futures(as seen in Java and other languages) for golang.

Promises is type-safe at runtime, and within an order of magnitude of performance of a solution built with pure channels and goroutines.

For a more thorough introduction to the library, please check out WHY.md

Examples

Single promise:

p := promises.New(func() int {
  return 1
})
var resolved int
err := p.Wait(&resolved)

Chained Promise

p := promises.New(func() int {
  return 1
})
timesTwo := p.Then(func(x int) int {
  return x*2
})
var resolved int
err := timesTwo.Wait(&resolved)

Promise.all

p := promises.New(func() int {
  return 1
})
timesTwo := p.Then(func(x int) int {
  return x * 2
})
plusFour := timesTwo.Then(func(x int) int{
  return x + 4
})

all := promises.All(p, timesTwo, plusFour)
results := []int{}
err := all.Wait(&results)

Error handling

p := promises.New(http.Get, "http://example.com")
// Do other work while Get processes in a goroutine...
var r *http.Response
err := p.Wait(&r)
// Errors are swallowed by promises and returned by wait.

Documentation

Overview

Package promise builds something similar to JS style promises, or Futures(as seen in Java and other languages) for golang.

promise is type-safe at runtime, and within an order of magnitude of performance of a solution built with pure channels and goroutines.

For a more thorough introduction to the library, please check out https://github.com/garlicnation/promises/blob/master/blog_example/WHY.md

Examples

Single promise:

p := promise.New(func() int {
	return 1
})
var resolved int
err := p.Wait(&resolved)

Chained Promise:

p := promise.New(func() int {
	return 1
})
timesTwo := p.Then(func(x int) int {
	return x*2
})
var resolved int
err := timesTwo.Wait(&resolved)

Promise.all:

p := promise.New(func() int {
	return 1
})
timesTwo := p.Then(func(x int) int {
	return x * 2
})
plusFour := timesTwo.Then(func(x int) int{
	return x + 4
})

all := promise.All(p, timesTwo, plusFour)
results := []int{}
err := all.Wait(&results)

Error handling:

p := promise.New(http.Get, "http://example.com")
// Do other work while Get processes in a goroutine...
var r *http.Response
err := p.Wait(&r)
// Errors are swallowed by promise and returned by wait.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyErr

type AnyErr struct {
	// Errs contains the error of all passed promises
	Errs []error
	// LastErr contains the error of the last promise to fail.
	LastErr error
}

AnyErr returns when all promises passed to Any fail

func (*AnyErr) Error

func (err *AnyErr) Error() string

type Promise

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

A Promise represents an asynchronously executing unit of work

Example (Chain)
p := New(func() int {
	fmt.Println("second")
	time.Sleep(50 * time.Millisecond)
	return 7
})

pByTwo := p.Then(func(x int) int {
	fmt.Println("fifth")
	time.Sleep(100 * time.Millisecond)
	return x * 2
})

pByFour := p.Then(func(x int) int {
	fmt.Println("third")
	time.Sleep(50 * time.Millisecond)
	return x * 4
})

var resOne, resTwo int
fmt.Println("First this happens")
err := pByTwo.Wait(resOne)
if err != nil {
	panic(err)
}
fmt.Println("fourth")
err = pByFour.Wait(&resTwo)
if err != nil {
	panic(err)
}
fmt.Println("sixth")
Output:

func All

func All(promises ...*Promise) *Promise

All returns a promise that resolves if all of the passed promises succeed or fails if any of the passed promises panics.

func Any

func Any(promises ...*Promise) *Promise

Any returns a promise that resolves if any of the passed promises succeed or fails if all of the passed promises panics. All of the supplied promises must be of the same type.

func New

func New(f interface{}, args ...interface{}) *Promise

New returns a promise that resolves when f completes. Any panic() encountered will be returned as an error from Wait()

func Race

func Race(promises ...*Promise) *Promise

Race returns a promise that resolves if any of the passed promises succeed or fails if any of the passed promises panics. All of the supplied promises must be of the same type.

func (*Promise) Lock

func (*Promise) Lock()

func (*Promise) Then

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

Then returns a promise that begins execution when this Promise completes

func (*Promise) Unlock

func (*Promise) Unlock()

func (*Promise) Wait

func (p *Promise) Wait(out ...interface{}) error

Wait blocks until the promise finishes execution or panics. If the promise panics, wait wraps the panic and returns an error.

Directories

Path Synopsis
blog_example

Jump to

Keyboard shortcuts

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