TL;DR: This package provides a small
Task[T]
type withAwait
, cancellation, panic-to-error conversion, and Promise-style combinators likeMap
,Then
,Catch
,Finally
,All
,Any
, andRace
. Context-aware and avoids goroutine leaks.
Why: I like to create stuff without any particular reason.
go get github.com/unkn0wn-root/go-async
ctx := context.Background()
userTask := async.Start(ctx, func(ctx context.Context) (User, error) {
// expensive work...
return loadUser(ctx, 42)
})
// Await with cancellation from caller:
user, err := userTask.Await(ctx)
if err != nil { /* handle */ }
// Chain transformations:
nameTask := async.Map(ctx, userTask, func(ctx context.Context, u User) (string, error) {
return strings.ToUpper(u.Name), nil
})
name, _ := nameTask.Await(ctx)
Map
,Then
,Catch
,Finally
All
/AllCancelOnError
AllSettled
Any
(first success)Race
(first completion)Delay
NewCompleter
(externally resolve/reject a Task)
See the examples in examples_test.go
.
- Context-propagation: every
Task
runs with its own derived context. - Cancellation:
Task.Cancel()
cancels the task’s context,Await(ctx)
also obeys the caller’s context (dual cancellation). - No leaks: all
Await
-based combinators use the caller’s context to stop waiting; functions suffixed withCancel
will also explicitly callCancel()
on remaining tasks to encourage prompt shutdown. - Panics become errors (
*async.PanicError
) with stack trace. - Race-safety: all result writes happen-before
Done
is closed. - Zero dependencies: only standard library.
- This is not a replacement for channels at all.
- For best cancellation behavior in groups, create child tasks with the same
parent
ctx
you pass into combinators.