Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func attempt<T>(_ behavior: RepeatBehavior, _ body: @escaping () -> Promise<T>) -> Promise<T> {
- var attempts: UInt = 0
- func attempt() -> Promise<T> {
- attempts += 1
- return body().recover({ error -> Promise<T> in
- let (maxCount, delay) = behavior.calculateConditions(attempts)
- guard attempts < maxCount else { throw error }
- return after(delay).then(on: nil, attempt)
- })
- }
- return attempt()
- }
- let behavior = RepeatBehavior.exponentialDelayed(maxCount: 5, initial: 1, multiplier: 0.5)
- attempt(behavior) {
- flakeyTask(parameters: foo)
- }.then {
- //…
- }.catch { _ in
- // we attempted three times but still failed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement