Timeout
Overview
Timeout runs fn with a deadline derived from ctx and timeout.
Part of the async package — Node.js analog: Promise.all, timers.
Signature
go
func Timeout(ctx context.Context, timeout time.Duration, fn func(context.Context) error) errorCompare: Node.js · Standard Go · gox
js
// Typical Promise.all, timers pattern in Node.jsgo
// Loop with backoff or context.WithTimeoutgo
import "github.com/sahilkhaire/gox/async"
err := async.Timeout(ctx, 5*time.Second, func(ctx context.Context) error {
return work(ctx)
})Example
go
import "github.com/sahilkhaire/gox/async"
err := async.Timeout(ctx, 5*time.Second, func(ctx context.Context) error {
return work(ctx)
})Tips
All async helpers respect context cancellation — prefer them over raw goroutines when you need timeouts.
Standard library alternative
gox wraps the Go standard library or a trusted dependency with Node-familiar naming. You can use the underlying library directly — see the package overview for escape hatches.