Documentation ¶
Overview ¶
Package promise contains some functions to support async programming.
Index ¶
- type Promise
- func All[T any](promises []*Promise[T]) *Promise[[]T]
- func Any[T any](promises []*Promise[T]) *Promise[T]
- func Catch[T any](promise *Promise[T], rejection func(err error) error) *Promise[T]
- func New[T any](runnable func(resolve func(T), reject func(error))) *Promise[T]
- func Race[T any](promises []*Promise[T]) *Promise[T]
- func Reject[T any](err error) *Promise[T]
- func Resolve[T any](resolution T) *Promise[T]
- func Then[T1, T2 any](promise *Promise[T1], resolve1 func(value T1) T2) *Promise[T2]
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Then ¶
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]) Catch ¶
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 ¶
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