Skip to content

Typed Task[T] for Go that brings async/await-style APIs - context-aware cancellation, panic-to-error and promise-like combinators such as Map, All, Race and more.

License

Notifications You must be signed in to change notification settings

unkn0wn-root/go-async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typed Tasks with async/await-like for Go

TL;DR: This package provides a small Task[T] type with Await, cancellation, panic-to-error conversion, and Promise-style combinators like Map, Then, Catch, Finally, All, Any, and Race. Context-aware and avoids goroutine leaks.

Why: I like to create stuff without any particular reason.

Install

go get github.com/unkn0wn-root/go-async

Quick start

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)

Combinators

  • 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.

Notes

  • 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 with Cancel will also explicitly call Cancel() 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.

Caveats

  • 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.

About

Typed Task[T] for Go that brings async/await-style APIs - context-aware cancellation, panic-to-error and promise-like combinators such as Map, All, Race and more.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages