go-async is a library that aims to simplify the way of working with multiple concurrent tasks
$ go get -u github.com/slavaavr/go-async- Task with a signature
func() (T, error)
group, ctx := async.NewGroup(context.Background())
defer group.Close()
task := async.Submit(group, func() (string, error) {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
return "hello World", nil
})
v, err := task.Await()
if err != nil {
panic(err)
}
println(v)- Action Task with a signature
func() error
group, ctx := async.NewGroup(context.Background())
defer group.Close()
task := async.SubmitAction(group, func() error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
println("executing a task without a response...")
return nil
})
if err := task.Await(); err != nil {
panic(err)
}- The
Groupentity ensures that all tasks are properly waited on, preventinggoroutine leakseven if one task returns anerrorand stops further execution. - For more use cases, see the examples folder.