Skip to content

Commit 8be672a

Browse files
stephencelisamartini51xwu
authored
Add Task.sleep(for: Duration) (#59203)
* Add Task.sleep(for: Duration) * Update TaskSleepDuration.swift * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <[email protected]> * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <[email protected]> * Fix suggestion. * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Xiaodi Wu <[email protected]> * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <[email protected]> Co-authored-by: Xiaodi Wu <[email protected]>
1 parent 528bd11 commit 8be672a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

stdlib/public/Concurrency/TaskSleepDuration.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,19 @@ 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+
public static func sleep(for duration: Duration) async throws {
158+
try await sleep(until: .now + duration, clock: .continuous)
159+
}
145160
}

0 commit comments

Comments
 (0)