Skip to content

Commit

Permalink
Update README.md docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Mar 18, 2024
1 parent 8a2b618 commit a2df0b7
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,11 @@ var sum int
c := taskgroup.NewCollector(func(v int) { sum += v })
```

Internally, a `Collector` wraps a solo task and a channel to receive results.

The `Task` and `NoError` methods of the collector `c` can then be used to wrap
a function that reports a value. If the function reports an error, that error
is returned from the task as usual. Otherwise, its non-error value is given to
the callback. As in the above example, calls to the function are serialized so
that it is safe to access state without additional locking:
The `Task`, `NoError`, and `Report`methods of the collector `c` can then be
used to wrap a function that reports a value. If the function reports an error,
that error is returned from the task as usual. Otherwise, its non-error value
is given to the callback. As in the above example, calls to the function are
serialized so that it is safe to access state without additional locking:

```go
// Report an error, no value for the collector.
Expand All @@ -291,14 +289,21 @@ g.Go(c.Task(func() (int, error) {

// Report a random integer to the collector.
g.Go(c.NoError(func() int { return rand.Intn(1000) })

// Report multiple values to the collector.
g.Go(c.Report(func(report func(int)) error {
report(10)
report(20)
report(30)
return nil
}))
```

Once all the tasks are done, call `Wait` to stop the collector and wait for it
to finish:
Once all the tasks derived from the collector are done, it is safe to access
the values accumulated by the callback:

```go
g.Wait() // wait for tasks to finish
c.Wait() // wait for the collector to finish

// Now you can access the values accumulated by c.
fmt.Println(sum)
Expand Down

0 comments on commit a2df0b7

Please sign in to comment.