Back to Lo

Core Attempt

docs/data/core-attempt.md

1.53.0994 B
Original Source

Invokes a function up to N times until it returns nil. Returns the number of iterations attempted (1-based) and the last error. Useful for retrying operations that might fail temporarily.

go
// Success after 3 attempts
iter, err := lo.Attempt(5, func(i int) error {
    if i == 2 {
        return nil // succeeds on 3rd attempt (index 2)
    }
    return errors.New("failed")
})
// iter: 3, err: nil
go
// All attempts fail - returns last error
iter, err = lo.Attempt(3, func(i int) error {
    return fmt.Errorf("attempt %d failed", i)
})
// iter: 3, err: "attempt 2 failed" (last error from index 2)
go
// Immediate success on first attempt
iter, err = lo.Attempt(5, func(i int) error {
    return nil // succeeds immediately
})
// iter: 1, err: nil
go
// Zero attempts - returns error without calling function
iter, err = lo.Attempt(0, func(i int) error {
    return errors.New("should not be called")
})
// iter: 0, err: "maxIteration must be greater than 0"