Created
April 11, 2024 22:16
-
-
Save tkersey/219cec91cdea7c7c5433dc1a99dc967e to your computer and use it in GitHub Desktop.
try await Task.sleep(until: targetDate)
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 | |
extension Task { | |
static func sleep(until target: Date) async throws where Success == Never, Failure == Never { | |
let duration = target.timeIntervalSinceNow | |
try await Self.sleep(for: .seconds(duration)) | |
} | |
} | |
print("Waiting for 1s...") | |
try await Task.sleep(until: Date.now.addingTimeInterval(1)) | |
print("... done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment