Documentation
¶
Index ¶
- Variables
- func SetDefaultPool(p Pool)
- type Pool
- type Promise
- func All[T any](ctx context.Context, promises ...*Promise[T]) *Promise[[]T]
- func AllWithPool[T any](ctx context.Context, pool Pool, promises ...*Promise[T]) *Promise[[]T]
- func New[T any](executor func(resolve func(T), reject func(error))) *Promise[T]
- func NewWithPool[T any](executor func(resolve func(T), reject func(error)), pool Pool) *Promise[T]
- func Race[T any](ctx context.Context, promises ...*Promise[T]) *Promise[T]
- func RaceWithPool[T any](ctx context.Context, pool Pool, promises ...*Promise[T]) *Promise[T]
- func (p *Promise[T]) Await(ctx context.Context) (T, error)
- func (p *Promise[T]) Catch(ctx context.Context, recoverValue func(error) (T, error)) *Promise[T]
- func (p *Promise[T]) CatchWithPool(ctx context.Context, recoverValue func(error) (T, error), pool Pool) *Promise[T]
- func (p *Promise[T]) Then[U any](ctx context.Context, resolve func(T) (U, error)) *Promise[U]
- func (p *Promise[T]) ThenWithPool[U any](ctx context.Context, resolve func(T) (U, error), pool Pool) *Promise[U]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNilRejection = errors.New("promise: rejected with nil error")
ErrNilRejection is returned when an executor calls reject(nil).
Functions ¶
func SetDefaultPool ¶
func SetDefaultPool(p Pool)
SetDefaultPool changes the pool for subsequently created promises and chains. It is safe to call concurrently with promise operations. p must not be nil.
Types ¶
type Pool ¶
type Pool interface {
Go(f func())
}
Pool schedules promise work. Go must run f exactly once or panic if submission fails; promises convert submission panics to rejections.
func FromAntsPool ¶
FromAntsPool adapts an ants pool. Failed submissions reject the promise. A nil pool panics synchronously.
func FromConcPool ¶
FromConcPool adapts a conc pool. A nil pool panics synchronously.
type Promise ¶
type Promise[T any] struct { // contains filtered or unexported fields }
Promise represents the eventual completion (or failure) of an asynchronous operation. Create promises with New or NewWithPool; the zero value is not usable. A Promise must not be copied after first use.
func All ¶
All resolves with results in input order, or rejects upon any rejection. It uses the default pool. Nil contexts, empty inputs, and nil or uninitialized input promises panic synchronously, before observing any inputs.
func AllWithPool ¶
AllWithPool dispatches settlement through custom pools. Dependency waits do not occupy pool workers. It otherwise behaves like All; a nil pool also panics.
func New ¶
New runs executor using the default pool, initially one goroutine per executor. The first call to resolve or reject wins. Panics are converted to rejections. Calling reject(nil) rejects with ErrNilRejection. A nil executor panics. Returning from executor alone does not settle the promise; it may arrange to call resolve or reject later, including from another goroutine.
func NewWithPool ¶
NewWithPool behaves like New, using pool to run executor. The pool determines whether execution is asynchronous. A nil executor or pool panics. Submission failures are returned by Await as errors.
func Race ¶
Race resolves or rejects when an input first becomes observable as settled. When multiple inputs are already settled, any one may win. It uses the default pool. Nil contexts, empty inputs, and nil or uninitialized input promises panic synchronously, before observing any inputs.
func RaceWithPool ¶
RaceWithPool dispatches settlement through custom pools. Dependency waits do not occupy pool workers. It otherwise behaves like Race; a nil pool also panics.
func (*Promise[T]) Await ¶
Await returns the resolved value and nil, or the zero value of T and an error. Canceling this wait does not cancel or settle the source promise. A context already canceled on entry takes precedence over a settled result; otherwise concurrent settlement and cancellation may be observed in either order. The value is copied, with Go's normal sharing for slices, maps, and pointers. A nil context, nil receiver, or uninitialized promise panics synchronously.
func (*Promise[T]) Catch ¶
Catch recovers a source rejection with a value of type T, or returns a new error. Successful source values pass through without calling recoverValue. A callback error discards its value. Cancellation of ctx skips recoverValue; a context error from the source is recoverable while ctx remains active. Callback panics become rejections. A nil context or callback, or a nil or uninitialized receiver, panics synchronously.
Example ¶
package main
import (
"context"
"fmt"
"strconv"
"github.com/chebyrash/promise"
)
func main() {
ctx := context.Background()
p := promise.New(func(_ func(int), reject func(error)) {
reject(fmt.Errorf("temporary failure"))
})
recovered := p.Catch(ctx, func(err error) (int, error) {
return 42, nil
})
value, err := recovered.Then(ctx, func(n int) (string, error) {
return strconv.Itoa(n), nil
}).Await(ctx)
fmt.Println(value, err)
}
Output: 42 <nil>
func (*Promise[T]) CatchWithPool ¶
func (p *Promise[T]) CatchWithPool(ctx context.Context, recoverValue func(error) (T, error), pool Pool) *Promise[T]
CatchWithPool behaves like Catch, using pool to run the recovery handler once p is ready. A nil pool panics, as do the invalid arguments described by Catch.
func (*Promise[T]) Then ¶
Then transforms a resolved value of type T into a value of type U. A rejected source propagates its error without calling resolve. A callback error discards the callback's value. Cancellation observed before invocation skips resolve. Panics in resolve become rejections. A nil context or callback, or a nil or uninitialized receiver, panics synchronously.
Example ¶
package main
import (
"context"
"fmt"
"strconv"
"github.com/chebyrash/promise"
)
func main() {
ctx := context.Background()
p := promise.New(func(resolve func(int), _ func(error)) { resolve(42) })
text := p.Then(ctx, func(n int) (string, error) {
return strconv.Itoa(n), nil
})
length := text.Then(ctx, func(s string) (int, error) { return len(s), nil })
value, err := length.Await(ctx)
fmt.Println(value, err)
}
Output: 2 <nil>