Skip to content

Commit

Permalink
taskgroup: add a Run method and a top-level Run function
Browse files Browse the repository at this point in the history
This is a convenience for tasks that do not report an error.
  • Loading branch information
creachadair committed Sep 6, 2024
1 parent 219cc46 commit 7cdd5c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions single.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func Go[T any](task func() T) *Single[T] {
return &Single[T]{valc: valc}
}

// Run runs task in a new goroutine. The caller must call Wait to wait for the
// task to return and collect its error. This is shorthand for:
//
// taskgroup.Go(taskgroup.NoError(task))
//
// The error reported by Wait is always nil.
func Run(task func()) *Single[error] { return Go(NoError(task)) }

// Call starts task in a new goroutine. The caller must call Wait to wait for
// the task to return and collect its result.
func Call[T any](task func() (T, error)) *Single[Result[T]] {
Expand Down
6 changes: 6 additions & 0 deletions taskgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func (g *Group) Go(task Task) *Group {
return g
}

// Run runs task in a new goroutine in g, and returns g to permit chaining.
// This is shorthand for:
//
// g.Go(taskgroup.NoError(task))
func (g *Group) Run(task func()) *Group { return g.Go(NoError(task)) }

func (g *Group) handleError(err error) {
g.μ.Lock()
defer g.μ.Unlock()
Expand Down

0 comments on commit 7cdd5c2

Please sign in to comment.