asynctask

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 6 Imported by: 2

README

AsyncTask

Build Go Report Card GoDoc Codecov

Simple mimik of async/await for those come from C# world, so you don't need to dealing with waitGroup/channel in golang.

also the result is strongTyped with go generics, no type assertion is needed.

few chaining method provided:

  • ContinueWith: send task1's output to task2 as input, return reference to task2.
  • AfterBoth : send output of taskA, taskB to taskC as input, return reference to taskC.
  • WaitAll: all of the task have to finish to end the wait (with an option to fail early if any task failed)
  • WaitAny: any of the task finish would end the wait
    // start task
    task := asynctask.Start(ctx, countingTask)
    
    // do something else
    somethingelse()
    
    // get the result
    rawResult, err := task.Wait()
    // or
    task.Cancel()

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCanceled = errors.New("canceled")

ErrCanceled is returned if a cancel is triggered

View Source
var ErrPanic = errors.New("panic")

ErrPanic is returned if panic cought in the task

Functions

func ActionToFunc added in v1.3.0

func ActionToFunc(action func(context.Context) error) func(context.Context) (interface{}, error)

ActionToFunc 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 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

type AfterBothFunc[T, S, R any] func(context.Context, T, S) (R, error)

AfterBothFunc is a function that has 2 input.

type AsyncFunc

type AsyncFunc[T any] func(context.Context) (T, error)

AsyncFunc is a function interface this asyncTask accepts.

type ContinueFunc added in v0.5.1

type ContinueFunc[T any, S any] func(context.Context, T) (S, error)

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

func (s State) IsTerminalState() bool

IsTerminalState tells whether the task finished

type Task added in v1.0.0

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

Task is a handle to the running function. which you can use to wait, cancel, get the result.

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 ContinueWith[T any, S any](ctx context.Context, tsk *Task[T], next ContinueFunc[T, S]) *Task[S]

func NewCompletedTask added in v0.4.0

func NewCompletedTask[T any](value T) *Task[T]

NewCompletedTask returns a Completed task, with result=nil, error=nil

func Start

func Start[T any](ctx context.Context, task AsyncFunc[T]) *Task[T]

Start run a async function and returns you a handle which you can Wait or Cancel. context passed in may impact task lifetime (from context cancellation)

func (*Task[T]) Cancel added in v1.0.0

func (t *Task[T]) Cancel() bool

Cancel the task by cancel the context. !! this rely on the task function to check context cancellation and proper context handling.

func (*Task[T]) Result added in v1.0.0

func (t *Task[T]) Result(ctx context.Context) (T, error)

func (*Task[T]) State added in v1.0.0

func (t *Task[T]) State() State

State return state of the task.

func (*Task[T]) Wait added in v1.0.0

func (t *Task[T]) Wait(ctx context.Context) error

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

func (t *Task[T]) WaitWithTimeout(ctx context.Context, timeout time.Duration) (T, error)

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

type Waitable added in v1.0.0

type Waitable interface {
	Wait(context.Context) error
}

Jump to

Keyboard shortcuts

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