Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Last active December 17, 2022 01:56
Show Gist options
  • Save boraseoksoon/c2f9a5292076a927178c007ad4654a39 to your computer and use it in GitHub Desktop.
Save boraseoksoon/c2f9a5292076a927178c007ad4654a39 to your computer and use it in GitHub Desktop.
delay
import Foundation
// >= iOS 16
var task: Task<(), Never>?
func delay(interval: Duration = .seconds(1),
operation: @escaping () -> Void) {
task = Task {
do {
try await Task.sleep(for: interval)
operation()
} catch {
// TODO
}
}
}
delay {
print("go!")
}
//delay(.seconds(2)) {
// print("go!")
//}
for i in 0...5 {
delay(interval: .seconds(5)) {
print(i)
}
}
// 5 seconds later.
// 0
// 1
// 2
// 3
// 4
// 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment