Created
March 21, 2018 11:39
-
-
Save olbrichj/1f9586ef28776db90e8044233a40d628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
fetch(url: url) | |
}.then { | |
//… | |
}.catch { _ in | |
// we still failed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will your code retry for .then block?