Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Last active December 16, 2022 21:06
Show Gist options
  • Save boraseoksoon/898433f2ffb55b0d6fd223517ed0f167 to your computer and use it in GitHub Desktop.
Save boraseoksoon/898433f2ffb55b0d6fd223517ed0f167 to your computer and use it in GitHub Desktop.
delay prior to iOS 16
import Foundation
// < iOS 16
var task: Task<(), Never>?
func delay(seconds: Int = 1,
operation: @escaping () -> Void) {
task = Task {
do {
try await Task.sleep(seconds: seconds)
operation()
} catch {
// TODO
}
}
}
delay {
print("go!")
}
extension Task where Success == Never, Failure == Never {
static func sleep(seconds: Int) async throws {
let duration = UInt64(Double(seconds) * 1_000_000_000)
try await Task.sleep(nanoseconds: duration)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment