Documentation
¶
Index ¶
- Variables
- func All(funcs ...AsyncFn) ([][]any, error)
- func AllCompleted(funcs ...AsyncFn) ([][]any, error)
- func AllCompletedWithContext(ctx context.Context, funcs ...AsyncFn) ([][]any, error)
- func AllWithContext(ctx context.Context, funcs ...AsyncFn) ([][]any, error)
- func Forever(fn ForeverFn) error
- func ForeverWithContext(ctx context.Context, fn ForeverFn) error
- func Parallel(concurrency int, funcs ...AsyncFn) ([][]any, error)
- func ParallelCompleted(concurrency int, funcs ...AsyncFn) ([][]any, error)
- func ParallelCompletedWithContext(ctx context.Context, concurrency int, funcs ...AsyncFn) ([][]any, error)
- func ParallelWithContext(ctx context.Context, concurrency int, funcs ...AsyncFn) ([][]any, error)
- func Race(funcs ...AsyncFn) ([]any, int, error)
- func RaceWithContext(ctx context.Context, funcs ...AsyncFn) ([]any, int, error)
- type AsyncFn
- type ExecutionError
- type ExecutionErrors
- type ForeverFn
Constants ¶
This section is empty.
Variables ¶
var ( // ErrContextCanceled to indicate the context was canceled or timed out. ErrContextCanceled error = errors.New("context canceled") // ErrInvalidConcurrency to indicate the number of the concurrency limitation is an invalid // value. ErrInvalidConcurrency error = errors.New("invalid concurrency") // ErrNotFunction indicates the value is not a function. ErrNotFunction error = errors.New("not function") )
Functions ¶
func All ¶
All executes the functions asynchronously until all functions have been finished. If some function returns an error or panic, it will immediately return an execution error, and send a cancel signal to all other functions by context.
The index of the function will be -1 if all functions have been completed without error or panic.
out, err := async.All(func() (int, error) { return 1, nil }, func() (string, error) { time.Sleep(100 * time.Millisecond) return "hello", nil }, func(ctx context.Context) error { time.Sleep(50 * time.Millisecond) return nil }) // out: [][]any{{1, nil}, {"hello", nil}, {nil}} // err: nil _, err = async.All(func() (int, error) { return 0, errors.New("some error") }, func() (string, error) { time.Sleep(100 * time.Millisecond) return "hello", nil }) // err: function 0 error: some error
func AllCompleted ¶
AllCompleted executes the functions asynchronously until all functions have been finished. It will return an error slice that is ordered by the functions order, and a boolean value to indicate whether any functions return an error or panic.
out, err := async.AllCompleted(func() (int, error) { return 1, nil }, func() (string, error) { time.Sleep(100 * time.Millisecond) return "hello", nil }, func(ctx context.Context) error { time.Sleep(50 * time.Millisecond) return errors.New("some error") }) // out: [][]any{{1, nil}, {"hello", nil}, {some error}} // err: function 2 error: some error
func AllCompletedWithContext ¶
AllCompletedWithContext executes the functions asynchronously until all functions have been finished, or the context is done (canceled or timeout). It will return an error slice that is ordered by the functions order, and a boolean value to indicate whether any functions return an error or panic.
func AllWithContext ¶
AllWithContext executes the functions asynchronously until all functions have been finished, or the context is done (canceled or timeout). If some function returns an error or panic, it will immediately return an execution error and send a cancel signal to all other functions by context.
The index of the function will be -1 if all functions have been completed without error or panic, or the context has been canceled (or timeout) before all functions finished.
func Forever ¶ added in v0.3.0
Forever runs the function indefinitely until the function panics or returns an error.
You can use the context and call the next function to pass values to the next invocation. The next function can be invoked one time only, and it will have no effect if it is invoked again.
err := Forever(func(ctx context.Context, next func(context.Context)) error { v := ctx.Value("key") if v != nil { vi := v.(int) if vi == 3 { return errors.New("finish") } fmt.Printf("value: %d\n", vi) next(context.WithValue(ctx, "key", vi+1)) } else { next(context.WithValue(ctx, "key", 1)) } return nil }) fmt.Printf("err: %v\n", err) // value: 1 // value: 2 // err: finish
func ForeverWithContext ¶ added in v0.3.0
ForeverWithContext runs the function indefinitely until the function panics or returns an error.
You can use the context and call the next function to pass values to the next invocation. The next function can be invoked one time only, and it will have no effect if it is invoked again.
func Parallel ¶ added in v0.2.0
Parallel runs the functions asynchronously with the specified concurrency limitation. It will send a cancel sign to context and terminate immediately if any function returns an error or panic, and also returns an execution error to indicate the error.
The number of concurrency must be greater than or equal to 0, and it means no concurrency limitation if the number is 0.
// Run 2 functions asynchronously at the time. out, err := Parallel(2, func(ctx context.Context) (int, error) { // Do something return 1, nil }, func(ctx context.Context) (string, error) { // Do something return "hello", nil }, func(ctx context.Context) error { // Do something return nil } /* , ... */) // out: [][]any{{1, <nil>}, {"hello", <nil>}, {<nil>}} // err: <nil>
func ParallelCompleted ¶ added in v0.2.0
ParallelCompleted runs the functions asynchronously with the specified concurrency limitation. It returns an error array and a boolean value to indicate whether any function panics or returns an error, and you can get the error details from the error array by the indices of the functions in the parameter list. It will return until all of the functions are finished.
The number of concurrency must be greater than or equal to 0, and it means no concurrency limitation if the number is 0.
func ParallelCompletedWithContext ¶ added in v0.2.0
func ParallelCompletedWithContext( ctx context.Context, concurrency int, funcs ...AsyncFn, ) ([][]any, error)
ParallelCompletedWithContext runs the functions asynchronously with the specified concurrency limitation and the context. It returns an error array and a boolean value to indicate whether any function panics or returns an error, and you can get the error details from the error array by the indices of the functions in the parameter list. It will return until all of the functions are finished.
The number of concurrency must be greater than or equal to 0, and it means no concurrency limitation if the number is 0.
func ParallelWithContext ¶ added in v0.2.0
func ParallelWithContext( ctx context.Context, concurrency int, funcs ...AsyncFn, ) ([][]any, error)
ParallelWithContext runs the functions asynchronously with the specified concurrency limitation. It will send a cancel sign to context and terminate immediately if any function returns an error or panic, and also returns an execution error to indicate the error. If the context was canceled or timed out before all functions finished executing, it will send a cancel sign to all uncompleted functions, and return a context canceled error.
The number of concurrency must be greater than or equal to 0, and it means no concurrency limitation if the number is 0.
func Race ¶
Race executes the functions asynchronously, it will return the index and the result of the first of the finished function (including panic), and it will not send a cancel signal to other functions.
out, index, err := Race(func(ctx context.Context) (int, error) { request.Get("https://example.com") return 0, nil }, func(ctx context.Context) (string, error) { time.Sleep(500 * time.Millisecond) return "test", nil }) // If the first function faster than the second one: // out: []any{0, <nil>}, index: 0, err: <nil> // // Otherwise: // out: []any{"test", <nil>}, index: 1, err: <nil>
func RaceWithContext ¶
RaceWithContext executes the functions asynchronously, it will return the index and the result of the first of the finished function (including panic), and it will not send a cancel signal to other functions.
Types ¶
type AsyncFn ¶ added in v0.2.0
type AsyncFn any
AsyncFn is the function to run, the function can be a function without any restriction that accepts any parameters and any return values. For the best practice, please define the function like the following styles:
func(context.Context) error func(context.Context) (out_type, error) func(context.Context, in_type) error func(context.Context, in_type) (out_type, error) func(context.Context, in_type1, in_type2/*, ...*/) (out_type1, out_type_2,/* ...,*/ error)
type ExecutionError ¶ added in v0.3.0
type ExecutionError interface { // Index returns the function's index in the parameters list that the function had returned an // error or panicked. Index() int // Err returns the original error that was returned or panicked by the function. Err() error // Error returns the execution error message. Error() string }
type ExecutionErrors ¶ added in v0.3.0
type ExecutionErrors []ExecutionError
ExecutionErrors is an array of ExecutionError.
func (ExecutionErrors) Error ¶ added in v0.3.0
func (ee ExecutionErrors) Error() string
Error combines and returns all of the execution errors' message.