session-ios/SessionUtilitiesKit/Promise+Retrying.swift

15 lines
507 B
Swift
Raw Normal View History

2020-11-05 02:07:21 +01:00
import PromiseKit
/// Retry the promise constructed in `body` up to `maxRetryCount` times.
2020-11-05 02:15:57 +01:00
public func attempt<T>(maxRetryCount: UInt, recoveringOn queue: DispatchQueue, body: @escaping () -> Promise<T>) -> Promise<T> {
2020-11-05 02:07:21 +01:00
var retryCount = 0
func attempt() -> Promise<T> {
return body().recover(on: queue) { error -> Promise<T> in
guard retryCount < maxRetryCount else { throw error }
retryCount += 1
return attempt()
}
}
return attempt()
}