package waiterr_test import ( "context" "errors" "fmt" "time" "codeberg.org/danjones000/waiterr" ) func Example() { we := waiterr.New() we.Go(func() error { time.Sleep(100 * time.Millisecond) fmt.Println("Goroutine 1 finished") return nil }) we.Go(func() error { time.Sleep(50 * time.Millisecond) fmt.Println("Goroutine 2 finished with an error") return errors.New("something went wrong in goroutine 2") }) we.Go(func() error { time.Sleep(150 * time.Millisecond) fmt.Println("Goroutine 3 finished") return nil }) // Wait for all goroutines and get all errors if err := we.Wait(); err != nil { fmt.Printf("All goroutines finished. Combined error:\n%v\n", err) } // Output: // Goroutine 2 finished with an error // Goroutine 1 finished // Goroutine 3 finished // All goroutines finished. Combined error: // something went wrong in goroutine 2 } func ExampleWaitErr_WaitForError() { we := waiterr.New() we.Go(func() error { time.Sleep(100 * time.Millisecond) return errors.New("first error from we") }) we.Go(func() error { time.Sleep(50 * time.Millisecond) return errors.New("second error from we") }) if err := we.WaitForError(); err != nil { fmt.Printf("First error returned from we: %v\n", err) } // Output: // First error returned from we: second error from we } func ExampleWaitErr_Unwrap() { we := waiterr.New() we.Go(func() error { time.Sleep(100 * time.Millisecond) return errors.New("first error from we") }) we.Go(func() error { time.Sleep(50 * time.Millisecond) return errors.New("second error from we") }) we.Go(func() error { time.Sleep(75 * time.Millisecond) return nil }) _ = we.Wait() errs := we.Unwrap() for _, e := range errs { fmt.Println(e) } // Output: // second error from we // first error from we } func ExampleWithContext() { 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("Goroutine 1 finished") return nil case <-ctx.Done(): return ctx.Err() } }) we.Go(func() error { select { case <-time.After(50 * time.Millisecond): fmt.Println("Goroutine 2 finished with an error") return errors.New("something went wrong in goroutine 2") case <-ctx.Done(): return ctx.Err() } }) we.Go(func() error { select { case <-time.After(150 * time.Millisecond): fmt.Println("Goroutine 3 finished") return nil case <-ctx.Done(): return ctx.Err() } }) if err := we.Wait(); err != nil { fmt.Printf("All goroutines finished. Combined error: %s\n", err) } // Output: // Goroutine 2 finished with an error // All goroutines finished. Combined error: something went wrong in goroutine 2 // context canceled // context canceled }