-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Task.sleep(for: Duration)
#59203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5415270
076fc40
15b03fd
ccf8433
b930a38
354520b
78723b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,4 +142,19 @@ extension Task where Success == Never, Failure == Never { | |
) async throws { | ||
try await clock.sleep(until: deadline, tolerance: tolerance) | ||
} | ||
|
||
/// Suspends the current task for the given duration on a continuous clock. | ||
/// | ||
/// If the task is cancelled before the time ends, this function throws | ||
/// `CancellationError`. | ||
/// | ||
/// This function doesn't block the underlying thread. | ||
/// | ||
/// try await Task.sleep(for: .seconds(3)) | ||
/// | ||
stephencelis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// - Parameter duration: The duration to wait. | ||
@available(SwiftStdlib 5.7, *) | ||
public static func sleep(for duration: Duration) async throws { | ||
try await sleep(until: .now + duration, clock: .continuous) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me from the docs when one would reach for a suspending clock, so I went with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so suspending will mean that if the machine is asleep then the clock makes no forward progress so for example sleep for 5 seconds for the continuous clock it would work as such: sleep for 5 seconds There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my guess is that more often than not, this particular variant would be the latter (continuous as you had a intuition for). If developers want the alternate version then they can use the slightly more verbose one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That was my understanding, too, I just can't think through when a suspending clock would be preferred :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @phausler You said Continuous in the review thread: https://forums.swift.org/t/se-0329-second-review-clock-instant-and-duration/54509/3 . Is it possible to clarify this in the text of SE-0329? |
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.