Type MethodConcurrency

sleep(until:tolerance:clock:)

Suspends the current task until the given deadline within a tolerance.

  • Available when Success is Never and Failure is Never

  • iOS 9999+
  • macOS 9999+
  • tvOS 9999+
  • watchOS 9999+
static func sleep<C>(until deadline: C.Instant, tolerance: C.Instant.Duration? = nil, clock: C) async throws where C : Clock

Overview

If the task is canceled before the time ends, this function throws CancellationError.

This function doesn’t block the underlying thread.

  try await Task.sleep(until: .now + .seconds(3), clock: .continuous)