Race
Overview
Race runs tasks concurrently and returns the first successful result or error.
If you are coming from Node.js, the closest pattern is Promise.race([a(), b()]).
Signature
go
func Race[T any](ctx context.Context, tasks ...func(context.Context) (T, error)) (T, error)Compare: Node.js · Standard Go · gox
js
Promise.race([a(), b()])go
select {
case <-doneA:
case <-doneB:
case <-ctx.Done():
}go
import "github.com/sahilkhaire/gox/async"
async.Race(ctx, a, b)Example
go
import "github.com/sahilkhaire/gox/async"
async.Race(ctx, a, b)Tips
All async helpers respect context cancellation — prefer them over raw goroutines when you need timeouts.
Standard library alternative
Use the standard library directly:
go
select {
case <-doneA:
case <-doneB:
case <-ctx.Done():
}