import Foundation import PromiseKit // https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md func attemptNetworking( maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2), _ body: @escaping () -> Promise ) -> Promise { var attempts = 0 func attempt() -> Promise { attempts += 1 return body().recover { error -> Promise in guard attempts < maximumRetryCount, error is URLError else { throw error } if Current.isRunningTests { return attempt() } else { return after(delayBeforeRetry).then(on: .main, attempt) } } } return attempt() }