2020-11-05 02:07:21 +01:00
|
|
|
import PromiseKit
|
|
|
|
|
|
|
|
/// Delay the execution of the promise constructed in `body` by `delay` seconds.
|
2020-11-11 00:58:56 +01:00
|
|
|
public func withDelay<T>(_ delay: TimeInterval, completionQueue: DispatchQueue, body: @escaping () -> Promise<T>) -> Promise<T> {
|
2020-11-05 02:07:21 +01:00
|
|
|
let (promise, seal) = Promise<T>.pending()
|
2022-02-22 04:01:48 +01:00
|
|
|
Timer.scheduledTimerOnMainThread(withTimeInterval: delay, repeats: false) { _ in
|
|
|
|
body().done(on: completionQueue) {
|
|
|
|
seal.fulfill($0)
|
|
|
|
}.catch(on: completionQueue) {
|
|
|
|
seal.reject($0)
|
|
|
|
}
|
2020-11-05 02:07:21 +01:00
|
|
|
}
|
|
|
|
return promise
|
|
|
|
}
|