Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. func attempt<T>(_ behavior: RepeatBehavior, _ body: @escaping () -> Promise<T>) -> Promise<T> {
  2. var attempts: UInt = 0
  3. func attempt() -> Promise<T> {
  4. attempts += 1
  5. return body().recover({ error -> Promise<T> in
  6. let (maxCount, delay) = behavior.calculateConditions(attempts)
  7. guard attempts < maxCount else { throw error }
  8. return after(delay).then(on: nil, attempt)
  9. })
  10. }
  11. return attempt()
  12. }
  13.  
  14. let behavior = RepeatBehavior.exponentialDelayed(maxCount: 5, initial: 1, multiplier: 0.5)
  15. attempt(behavior) {
  16. flakeyTask(parameters: foo)
  17. }.then {
  18. //…
  19. }.catch { _ in
  20. // we attempted three times but still failed
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement