From 842d881269e7df17017d4186f4cae1354142102c Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Wed, 11 Sep 2024 18:45:39 -0700 Subject: [PATCH] README.md: demonstrate use of Trigger with a context --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index d5207f8..7db13c2 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,19 @@ g := taskgroup.New(func(err error) error { }) ``` +This mechanism can also be used to trigger a context cancellation if a task +fails, for example: + +```go +ctx, cancel := context.WithCancel(context.Background()) +defer cancel() + +g := taskgroup.New(taskgroup.Trigger(cancel)) +``` + +Now, if a task in `g` reports an error, it will cancel the context, allowing +any other running tasks to observe a context cancellation and bail out. + ## Controlling Concurrency The `Limit` method supports limiting the number of concurrently _active_