Last active
June 3, 2022 12:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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