Back to Lo

Core Attemptwhile

docs/data/core-attemptwhile.md

1.53.0304 B
Original Source

Invokes a function up to N times until it returns nil. The second return value controls whether to continue attempting.

go
count, err := lo.AttemptWhile(5, func(i int) (error, bool) {
    if i == 2 {
        return nil, false
    }
    return errors.New("fail"), true
})
// count == 3, err == nil