From 76c3e62228fb388d0ec403e8e56111ede7ccd616 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Nov 2025 15:14:43 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20WithContext=20example=20to?= =?UTF-8?q?=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index e7923a0..f9f4688 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,42 @@ func main() { } ``` +### Using WithContext + +```go +package main + +import ( + "context" + "fmt" + "time" + + "codeberg.org/danjones000/waiterr" +) + +func main() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + we, ctx := waiterr.WithContext(ctx) + + we.Go(func() error { + select { + case <-time.After(100 * time.Millisecond): + fmt.Println("Task completed") + return nil + case <-ctx.Done(): + fmt.Println("Task cancelled") + return ctx.Err() + } + }) + + _ = we.Wait() + // Output: + // Task completed +} +``` + ## Contributing Please refer to the [AGENTS.md](AGENTS.md) file for guidelines on contributing to this project, including code style, commit messages, and Git workflow.