Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func attempt<T>(maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2), _ body: @escaping () -> Promise<T>) -> Promise<T> {
- var attempts = 0
- func attempt() -> Promise<T> {
- attempts += 1
- return body().recover { error -> Promise<T> in
- guard attempts < maximumRetryCount else { throw error }
- return after(delayBeforeRetry).then(on: nil, attempt)
- }
- }
- return attempt()
- }
- attempt(maximumRetryCount: 3) {
- flakeyTask(parameters: foo)
- }.then {
- //…
- }.catch { _ in
- // we attempted three times but still failed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement