You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0374-clock-sleep-for.md
+27-5Lines changed: 27 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -142,7 +142,9 @@ extension Clock {
142
142
/// Suspends for the given duration.
143
143
///
144
144
/// Prefer to use the `sleep(until:tolerance:)` method on `Clock` if you have access to an
145
-
/// absolute instant.
145
+
/// absolute instant.
146
+
@available(SwiftStdlib 5.7, *)
147
+
@_alwaysEmitIntoClient
146
148
publicfuncsleep(
147
149
forduration: Duration,
148
150
tolerance: Duration?=nil
@@ -152,17 +154,37 @@ extension Clock {
152
154
}
153
155
```
154
156
155
-
<!-- We should make sure that we talk in the documentation about why this kind of method should only be used as a convenience API and why the primary API should traffic in absolute clock values. But with that said, I absolutely agree that it's pretty much always going to be useful to have this kind of convenience alongside that primary API, and so this proposal seems like a nice addition. -->
156
-
157
157
This will allow one to sleep for a duration with a clock rather than sleeping until an instant.
158
158
159
+
Further, to make the APIs between `clock.sleep` and `Task.sleep` similar, we will also add a `tolerance` argument to `Task.sleep(for:)`:
160
+
161
+
```swift
162
+
/// Suspends the current task for the given duration on a continuous clock.
163
+
///
164
+
/// If the task is cancelled before the time ends, this function throws
165
+
/// `CancellationError`.
166
+
///
167
+
/// This function doesn't block the underlying thread.
0 commit comments