Skip to content

Commit 8faef1a

Browse files
[SE-0329] Update the Task.sleep(for:) method (#60473)
(cherry picked from commit 624124d) Co-authored-by: Ben Rimmington <[email protected]>
1 parent 26a1ae7 commit 8faef1a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

stdlib/public/Concurrency/TaskSleepDuration.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,20 @@ extension Task where Success == Never, Failure == Never {
142142
) async throws {
143143
try await clock.sleep(until: deadline, tolerance: tolerance)
144144
}
145+
146+
/// Suspends the current task for the given duration on a continuous clock.
147+
///
148+
/// If the task is cancelled before the time ends, this function throws
149+
/// `CancellationError`.
150+
///
151+
/// This function doesn't block the underlying thread.
152+
///
153+
/// try await Task.sleep(for: .seconds(3))
154+
///
155+
/// - Parameter duration: The duration to wait.
156+
@available(SwiftStdlib 5.7, *)
157+
@_alwaysEmitIntoClient
158+
public static func sleep(for duration: Duration) async throws {
159+
try await sleep(until: .now + duration, clock: .continuous)
160+
}
145161
}

0 commit comments

Comments
 (0)