Skip to content

Instantly share code, notes, and snippets.

@tkersey
Created April 11, 2024 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkersey/219cec91cdea7c7c5433dc1a99dc967e to your computer and use it in GitHub Desktop.
Save tkersey/219cec91cdea7c7c5433dc1a99dc967e to your computer and use it in GitHub Desktop.
try await Task.sleep(until: targetDate)
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