Advertisement
Guest User

Untitled

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