Skip to content

help understanding when clock.sleep(for:) might throw #48

Answered by mbrandonw
jaredh159 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @jaredh159, I don't think it's documented, but you can look through the source code of ContinuousClock and other clocks to see that the only error ever thrown is CancellationError. And so it only throws if the surrounding async context is cancelled, such as your heartbeatTask. This would have been a very good use of typed throws to show that sleep only throws one kind of error.

I don't see any reason for you to force unwrap though. You can never be sure that you will never cancel the async context, and so it is better to not crash if that does happen. You can simply ignore it if you want with a do/catch, or you can let your Task be a throwing task.

Additionally, I would recommend using…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jaredh159
Comment options

@mbrandonw
Comment options

Answer selected by jaredh159
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants