Sleep
Overview
Sleep waits until duration elapses or ctx is cancelled.
If you are coming from Node.js, the closest pattern is await sleep(ms).
Signature
go
func Sleep(ctx context.Context, d time.Duration) errorCompare: Node.js · Standard Go · gox
js
await new Promise(r => setTimeout(r, 1000));go
select {
case <-time.After(time.Second):
case <-ctx.Done():
return ctx.Err()
}go
import "github.com/sahilkhaire/gox/async"
if err := async.Sleep(ctx, time.Second); err != nil { return err }Example
go
import "github.com/sahilkhaire/gox/async"
if err := async.Sleep(ctx, time.Second); err != nil { return err }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 <-time.After(time.Second):
case <-ctx.Done():
return ctx.Err()
}