Last active
December 17, 2022 01:56
-
-
Save boraseoksoon/c2f9a5292076a927178c007ad4654a39 to your computer and use it in GitHub Desktop.
delay
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(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