Skip to content

Instantly share code, notes, and snippets.

@VishwaiOSDev
Last active June 3, 2022 12:27
Show Gist options
  • Save VishwaiOSDev/4a956d950f722f8aeb53507caae8ba97 to your computer and use it in GitHub Desktop.
Save VishwaiOSDev/4a956d950f722f8aeb53507caae8ba97 to your computer and use it in GitHub Desktop.
If one wants to use Task.sleep it needs nanoseconds for sleep parameter. This extension will convert the seconds to nanoseconds. We can use sleep with seconds function instead of nanoseconds.
extension Task where Success == Never, Failure == Never {
static func sleep(seconds: TimeInterval) async throws {
let duration = UInt64(seconds * 1_000_000_000)
try await Task.sleep(nanoseconds: duration)
}
}
// Usage
try await Task.sleep(seconds: 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment