Skip to content

Commit 9e69a26

Browse files
authored
Update proposal from feedback. (#1825)
1 parent 657ef2f commit 9e69a26

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

proposals/0374-clock-sleep-for.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ extension Clock {
142142
/// Suspends for the given duration.
143143
///
144144
/// 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
146148
public func sleep(
147149
for duration: Duration,
148150
tolerance: Duration? = nil
@@ -152,17 +154,37 @@ extension Clock {
152154
}
153155
```
154156

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-
157157
This will allow one to sleep for a duration with a clock rather than sleeping until an instant.
158158

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.
168+
///
169+
/// try await Task.sleep(for: .seconds(3))
170+
///
171+
/// - Parameter duration: The duration to wait.
172+
@available(SwiftStdlib 5.7, *)
173+
@_alwaysEmitIntoClient
174+
public static func sleep(
175+
for duration: Duration,
176+
tolerance: C.Instant.Duration? = nil
177+
) async throws {
178+
try await sleep(until: .now + duration, tolerance: tolerance, clock: .continuous)
179+
}
180+
```
181+
159182
## Detailed design
160183

161184
## Source compatibility, effect on ABI stability, effect on API resilience
162185

163186
As this is an additive change, it should not have any compatibility, stability or resilience
164-
problems. The only potential problem would be if someone has already run into this shortcoming
165-
and decided to define their own `sleep(for:)` method on clocks.
187+
problems.
166188

167189
## Alternatives considered
168190

0 commit comments

Comments
 (0)