Skip to content

Commit

Permalink
Improve the Listen example; fix unordered example output.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Jan 13, 2024
1 parent d899218 commit a961c55
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"math/rand"
"strings"
"time"

"github.com/creachadair/taskgroup"
Expand Down Expand Up @@ -67,14 +68,24 @@ func ExampleTrigger() {
}

func ExampleListen() {
// The taskgroup itself will only report the first non-nil task error, but
// you can use an error listener used to accumulate all of them.
var all []error
g := taskgroup.New(taskgroup.Listen(func(e error) {
fmt.Println(e)
all = append(all, e)
}))
g.Go(func() error { return errors.New("heard you") })
fmt.Println(g.Wait()) // the error was preserved
// Output:
// heard you
// heard you
g.Go(func() error { return errors.New("badness 1") })
g.Go(func() error { return errors.New("badness 2") })
g.Go(func() error { return errors.New("badness 3") })

if err := g.Wait(); err == nil || !strings.Contains(err.Error(), "badness") {
log.Fatalf("Unexpected error: %v", err)
}
fmt.Println(errors.Join(all...))
// Unordered output:
// badness 1
// badness 2
// badness 3
}

func ExampleGroup_Limit() {
Expand Down Expand Up @@ -217,7 +228,7 @@ func ExampleCollector_Stream() {
}

c.Wait()
// Output unordered:
// Unordered output:
// even 0
// odd 1
// even 2
Expand Down

0 comments on commit a961c55

Please sign in to comment.