Documentation
¶
Index ¶
- Variables
- func ActionToFunc(action func(context.Context) error) func(context.Context) (any, error)
- func AfterBothActionToFunc[T, S any](action func(context.Context, T, S) error) func(context.Context, T, S) (interface{}, error)
- func ContinueActionToFunc[T any](action func(context.Context, T) error) func(context.Context, T) (interface{}, error)
- func WaitAll(ctx context.Context, options *WaitAllOptions, tasks ...Waitable) error
- func WaitAny(ctx context.Context, options *WaitAnyOptions, tasks ...Waitable) error
- type AfterBothFunc
- type AsyncFunc
- type ContinueFunc
- type State
- type Task
- func AfterBoth[T, S, R any](ctx context.Context, tskT *Task[T], tskS *Task[S], next AfterBothFunc[T, S, R]) *Task[R]
- func ContinueWith[T any, S any](ctx context.Context, tsk *Task[T], next ContinueFunc[T, S]) *Task[S]
- func NewCompletedTask[T any](value T) *Task[T]
- func Start[T any](ctx context.Context, task AsyncFunc[T]) *Task[T]
- type WaitAllOptions
- type WaitAnyOptions
- type Waitable
Constants ¶
This section is empty.
Variables ¶
var ErrCanceled = errors.New("canceled")
ErrCanceled is returned if a cancel is triggered
var ErrPanic = errors.New("panic")
ErrPanic is returned if panic cought in the task
Functions ¶
func ActionToFunc ¶ added in v1.3.0
ActionToFunc converts an Action to a Func (C# term), satisfying the AsyncFunc interface.
- An Action is a function that performs an operation without returning a value. - A Func is a function that performs an operation and returns a value.
The returned Func returns nil as the result and the original Action's error as the error value.
func AfterBothActionToFunc ¶ added in v1.3.0
func AfterBothActionToFunc[T, S any](action func(context.Context, T, S) error) func(context.Context, T, S) (interface{}, error)
AfterBothActionToFunc convert a Action to Func (C# term), to satisfy the AfterBothFunc interface.
Action is function that runs without return anything Func is function that runs and return something
func ContinueActionToFunc ¶ added in v1.3.0
func ContinueActionToFunc[T any](action func(context.Context, T) error) func(context.Context, T) (interface{}, error)
ContinueActionToFunc convert a Action to Func (C# term), to satisfy the AsyncFunc interface.
Action is function that runs without return anything Func is function that runs and return something
func WaitAll ¶ added in v0.5.0
func WaitAll(ctx context.Context, options *WaitAllOptions, tasks ...Waitable) error
WaitAll block current thread til all task finished. first error from any tasks passed in will be returned.
func WaitAny ¶ added in v1.5.0
func WaitAny(ctx context.Context, options *WaitAnyOptions, tasks ...Waitable) error
WaitAny block current thread til any of task finished. first error from any tasks passed in will be returned if FailOnAnyError is set. first task end without error will end wait and return nil
Types ¶
type AfterBothFunc ¶ added in v1.1.0
AfterBothFunc is a function that has 2 input.
type ContinueFunc ¶ added in v0.5.1
ContinueFunc is a function that can be connected to previous task with ContinueWith
type State ¶
type State string
State of a task.
const StateCanceled State = "Canceled"
StateCanceled indicate task got canceled.
const StateCompleted State = "Completed"
StateCompleted indicate task is finished.
const StateFailed State = "Failed"
StateFailed indicate task failed.
const StateRunning State = "Running"
StateRunning indicate task is still running.
func (State) IsTerminalState ¶ added in v0.4.0
IsTerminalState tells whether the task finished
type Task ¶ added in v1.0.0
type Task[T any] struct { // contains filtered or unexported fields }
Task represents a handle to a running function. It provides methods to wait for completion, cancel execution, and retrieve results or errors.
func AfterBoth ¶ added in v1.1.0
func AfterBoth[T, S, R any](ctx context.Context, tskT *Task[T], tskS *Task[S], next AfterBothFunc[T, S, R]) *Task[R]
AfterBoth runs the function after both 2 input task finished, and will be fed with result from 2 input task.
if one of the input task failed, the AfterBoth task will be failed and returned, even other one are still running.
func ContinueWith ¶ added in v1.0.0
func NewCompletedTask ¶ added in v0.4.0
NewCompletedTask returns a Completed task, with result=nil, error=nil
func Start ¶
Start starts an asynchronous task and returns a Task handle to manage it. The provided context can be used to cancel the task. You can use the returned Task to Wait for completion or Cancel the task.
func (*Task[T]) Cancel ¶ added in v1.0.0
Cancel cancels the task, by canceling the context. !! it relies on the task function to properly handle context cancellation. If the task has already finished, this method returns false.
func (*Task[T]) Wait ¶ added in v1.0.0
Wait block current thread/routine until task finished or failed. context passed in can terminate the wait, through context cancellation but won't terminate the task (unless it's same context)
func (*Task[T]) WaitWithTimeout ¶ added in v1.0.0
WaitWithTimeout block current thread/routine until task finished or failed, or exceed the duration specified. timeout only stop waiting, taks will remain running.
type WaitAllOptions ¶ added in v0.5.0
type WaitAllOptions struct { // FailFast set to true will indicate WaitAll to return on first error it sees. FailFast bool }
WaitAllOptions defines options for WaitAll function
type WaitAnyOptions ¶ added in v1.5.0
type WaitAnyOptions struct { // FailOnAnyError set to true will indicate WaitAny to return on first error it sees. FailOnAnyError bool }
WaitAnyOptions defines options for WaitAny function