Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParallelMap ¶
func ParallelMap[V, T any](values []V, maxWait time.Duration, mapper func(context.Context, V) (T, error)) ([]T, []error)
ParallelMap applies the given mapper to each element of the given slice in parallel. The results and errors are returned in the same order as the slice. If the context expires before all the mappers are finished, the remaining mappers are cancelled.
If a mapper fails, the error is returned in the errs slice. If a mapper succeeds, the result is returned in the results slice.
Example:
values := []int{1, 2, 3, 4, 5} results, errs := ParallelMap(values, time.Second, func(ctx context.Context, v V) (T, error) { return v * v, nil })
func WaitAll ¶
WaitAll waits for all Waitable to complete successfully or to fail. Cancelling the context will cause all Waitable to fail.
The results and errors are returned in the same order as the Waitable. If a Waitable fails, the error is returned in the errs slice. If a Waitable succeeds, the result is returned in the results slice. If a Waitable is cancelled, the result is returned in the results slice and the error is returned in the errs slice.
ctx (context) can be used to cancel the WaitAll. If nil is provided, a context with a deadline of 1 second is used. Remember to cancel the context when you're done with it.
Example:
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second * 3)) defer cancel() results, errs := WaitAll(ctx, funcs...)