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.