[go: up one dir, main page]

async

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 2 Imported by: 4

README

Async

Async is an async/await like task package for Go

License Tests Go Reference Codecov GitHub Release Go Report Card

Features

  • Wait/WaitAny/WaitN for Task and Action
  • context.Context with timeout, cancel support
  • Works with generic instead of interface{}

Tutorials

see more examples on tasks, actions or go.dev

Install async
  • install latest commit from main branch
go get github.com/yaitoo/async@main
  • install latest release
go get github.com/yaitoo/async@latest
Wait

wait all tasks to completed.

t := async.New[int](func(ctx context.Context) (int, error) {
		return 1, nil
	}, func(ctx context.Context) (int, error) {
		return 2, nil
	})

result, err, taskErrs := t.Wait(context.Background())


fmt.Println(result)  //[1,2] or [2,1]
fmt.Println(err) // nil
fmt.Println(taskErrs) //nil


WaitAny

wait any task to completed

t := async.New[int](func(ctx context.Context) (int, error) {
    time.Sleep(2 * time.Second)
		return 1, nil
	}, func(ctx context.Context) (int, error) {
		return 2, nil
	})

result, err, taskErrs := t.WaitAny(context.Background())

fmt.Println(result)  //2
fmt.Println(err) //nil
fmt.Println(taskErrs) //nil

WaitN

wait N tasks to completed.

t := async.New[int](func(ctx context.Context) (int, error) {
    time.Sleep(2 * time.Second)
		return 1, nil
	}, func(ctx context.Context) (int, error) {
		return 2, nil
	}, func(ctx context.Context) (int, error) {
		return 3, nil
	})

result, err, taskErrs := t.WaitN(context.Background(),2)


fmt.Println(result)  //[2,3] or [3,2]
fmt.Println(err) //nil
fmt.Println(taskErrs) //nil

Timeout

cancel all tasks if it is timeout.

 t := async.New[int](func(ctx context.Context) (int, error) {
		time.Sleep(2 * time.Second)
		return 1, nil
	}, func(ctx context.Context) (int, error) {
		time.Sleep(2 * time.Second)
		return 2, nil
	})

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()

	result, err, tasks := t.WaitAny(ctx)
	//result, err, tasks := t.Wait(ctx)

	
	fmt.Println(result) //nil
	fmt.Println(err) // context.DeadlineExceeded
	fmt.Println(taskErrs) //nil
Cancel

manually cancel all tasks.

t := async.New[int](func(ctx context.Context) (int, error) {
    time.Sleep(2 * time.Second)
		return 1, nil
	}, func(ctx context.Context) (int, error) {
     time.Sleep(2 * time.Second)
		return 2, nil
	})

ctx, cancel := context.WithCancel(context.Background())
go func(){
  time.Sleep(1 * time.Second)
  cancel()
}()

//result, err, taskErrs := t.WaitAny(ctx)
 result, err, taskErrs := t.Wait(ctx)


fmt.Println(result)  //nil
fmt.Println(err) // context.Cancelled
fmt.Println(taskErrs) // nil


Contributing

Contributions are welcome! If you're interested in contributing, please feel free to contribute

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTooLessDone = errors.New("async: too less tasks/actions to completed without error")
)

Functions

This section is empty.

Types

type Action added in v1.0.4

type Action func(ctx context.Context) error

Action a task without result

type Awaiter

type Awaiter interface {
	// Add add an action
	Add(action Action)
	// Wait wail for all actions to completed
	Wait(context.Context) ([]error, error)
	// WaitAny wait for any action to completed without error, can cancel other tasks
	WaitAny(context.Context) ([]error, error)
	// WaitN wait for N actions to completed without error
	WaitN(context.Context, int) ([]error, error)
}

func NewA added in v1.0.4

func NewA(actions ...Action) Awaiter

NewA create an action awaiter

type Result

type Result[T any] struct {
	Data  T
	Error error
}

type Task added in v1.0.4

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

Task a task with result T

type Waiter added in v1.0.4

type Waiter[T any] interface {
	// Add add a task
	Add(task Task[T])
	// Wait wail for all tasks to completed
	Wait(context.Context) ([]T, []error, error)
	// WaitAny wait for any task to completed without error, can cancel other tasks
	WaitAny(context.Context) (T, []error, error)
	// WaitN wait for N tasks to completed without error
	WaitN(context.Context, int) ([]T, []error, error)
}

func New

func New[T any](tasks ...Task[T]) Waiter[T]

New create a task waiter

Jump to

Keyboard shortcuts

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