Back to Lo

Core Waitfor

docs/data/core-waitfor.md

1.53.0626 B
Original Source

Runs periodically until a condition is validated. Use WaitFor for simple predicates, or WaitForWithContext when you need context cancellation/timeout inside the predicate. Both return total iterations, elapsed time, and whether the condition became true.

go
iterations, elapsed, ok := lo.WaitFor(
    func(i int) bool {
        return i > 5
    },
    10*time.Millisecond,
    time.Millisecond,
)

With context:

go
iterations, elapsed, ok := lo.WaitForWithContext(
    context.Background(),
    func(_ context.Context, i int) bool {
        return i >= 5
    },
    10*time.Millisecond,
    time.Millisecond,
)