promise

package
v0.0.0-...-d1e1ff0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package promise contains some functions to support async programming.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Promise

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

Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value. ref : chebyrash/promise (https://github.com/chebyrash/promise) see js promise: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

func All

func All[T any](promises []*Promise[T]) *Promise[[]T]

All resolves when all of the promises have resolved, reject immediately upon any of the input promises rejecting.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	resolve("a")
})
p2 := New(func(resolve func(string), reject func(error)) {
	resolve("b")
})
p3 := New(func(resolve func(string), reject func(error)) {
	resolve("c")
})

pms := []*Promise[string]{p1, p2, p3}
p := All(pms)

result, err := p.Await()
if err != nil {
	return
}

fmt.Println(result)
Output:

[a b c]

func Any

func Any[T any](promises []*Promise[T]) *Promise[T]

Any resolves as soon as any of the input's Promises resolve, with the value of the resolved Promise. Any rejects if all of the given Promises are rejected with a combination of all errors.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	time.Sleep(time.Millisecond * 250)
	resolve("fast")
})
p2 := New(func(resolve func(string), reject func(error)) {
	time.Sleep(time.Millisecond * 500)
	resolve("slow")
})
p3 := New(func(resolve func(string), reject func(error)) {
	reject(errors.New("error"))
})

pms := []*Promise[string]{p1, p2, p3}
p := Any(pms)

result, err := p.Await()
if err != nil {
	return
}

fmt.Println(result)
Output:

fast

func Catch

func Catch[T any](promise *Promise[T], rejection func(err error) error) *Promise[T]

Catch allows to chain promises.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	err := errors.New("error1")
	reject(err)
})

p2 := Catch(p1, func(err error) error {
	e := errors.New("error2")
	return internal.JoinError(err, e)
})

_, err := p1.Await()

fmt.Println(err.Error())

result2, err := p2.Await()

fmt.Println(result2)
fmt.Println(err.Error())
Output:

error1

error1
error2

func New

func New[T any](runnable func(resolve func(T), reject func(error))) *Promise[T]

New create a new promise instance.

Example
p := New(func(resolve func(string), reject func(error)) {
	resolve("hello")
})

val, err := p.Await()
if err != nil {
	return
}

fmt.Println(val)
Output:

hello

func Race

func Race[T any](promises []*Promise[T]) *Promise[T]

Race will settle the first fullfiled promise among muti promises.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	time.Sleep(time.Millisecond * 100)
	resolve("fast")
})
p2 := New(func(resolve func(string), reject func(error)) {
	time.Sleep(time.Millisecond * 300)
	resolve("slow")
})

pms := []*Promise[string]{p1, p2}
p := Race(pms)

result, err := p.Await()
if err != nil {
	return
}

fmt.Println(result)
Output:

fast

func Reject

func Reject[T any](err error) *Promise[T]

Reject returns a Promise that has been rejected with a given error.

func Resolve

func Resolve[T any](resolution T) *Promise[T]

Resolve returns a Promise that has been resolved with a given value.

func Then

func Then[T1, T2 any](promise *Promise[T1], resolve1 func(value T1) T2) *Promise[T2]

Then allows chain calls to other promise methods.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	resolve("hello ")
})

p2 := Then(p1, func(s string) string {
	return s + "world"
})

result, err := p2.Await()
if err != nil {
	return
}

fmt.Println(result)
Output:

hello world

func (*Promise[T]) Await

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

Await blocks until the 'runable' to finish execution.

func (*Promise[T]) Catch

func (p *Promise[T]) Catch(reject func(error) error) *Promise[T]

Catch chain an existing promise with an intermediate reject function.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	err := errors.New("error1")
	reject(err)
})

p2 := p1.Catch(func(err error) error {
	e := errors.New("error2")
	return internal.JoinError(err, e)
})

_, err := p1.Await()

fmt.Println(err.Error())

result2, err := p2.Await()

fmt.Println(result2)
fmt.Println(err.Error())
Output:

error1

error1
error2

func (*Promise[T]) Then

func (p *Promise[T]) Then(resolve func(value T) T) *Promise[T]

Then allows chain calls to other promise methods.

Example
p1 := New(func(resolve func(string), reject func(error)) {
	resolve("hello ")
})

p2 := p1.Then(func(s string) string {
	return s + "world"
})

result, err := p2.Await()
if err != nil {
	return
}

fmt.Println(result)
Output:

hello world

Jump to

Keyboard shortcuts

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