Files
lo/docs/data/core-waitfor.md
T
2025-10-06 17:15:49 +02:00

1.4 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
WaitFor waitfor concurrency.go#L112 core concurrency https://go.dev/play/p/t_wTDmubbK3
core#concurrency#waitfor
core#concurrency#waitforwithcontext
core#retry#attemptwithdelay
core#retry#attemptwhilewithdelay
core#time#durationx
20
func WaitFor(condition func(i int) bool, timeout time.Duration, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool)
func WaitForWithContext(ctx context.Context, condition func(ctx context.Context, i int) bool, timeout time.Duration, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool)

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.

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

With context:

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