Type MethodConcurrency
sleep(for:)
Suspends the current task for the given duration on a continuous clock.
- iOS 16.0+
- macOS 13.0+
- tvOS 16.0+
- watchOS 9.0+
static func sleep(for duration: Duration) async throws
Parameters
- duration
The duration to wait.
Overview
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))