Skip to content
Node: await sleep(ms)github.com/sahilkhaire/gox/async

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) error

Compare: 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()
}

Back to async package overview

MIT Licensed · Built for Node.js developers moving to Go