Skip to content

Instantly share code, notes, and snippets.

@olbrichj
Created March 21, 2018 11:39
Show Gist options
  • Save olbrichj/1f9586ef28776db90e8044233a40d628 to your computer and use it in GitHub Desktop.
Save olbrichj/1f9586ef28776db90e8044233a40d628 to your computer and use it in GitHub Desktop.
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
}
@Erumaru
Copy link

Erumaru commented Apr 19, 2021

attempt {
     some()
}.then {
     other()
}

Will your code retry for .then block?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment