Last active
December 16, 2022 21:06
-
-
Save boraseoksoon/898433f2ffb55b0d6fd223517ed0f167 to your computer and use it in GitHub Desktop.
delay prior to iOS 16
This file contains hidden or 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
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