mirror of
https://github.com/logos-storage/nim-chronos.git
synced 2026-01-02 21:43:06 +00:00
* introduce user guide based on `mdbook` * set up structure for adding simple `chronos` usage examples * move most readme content to book * ci deploys book and api guide automatically * remove most of existing engine docs (obsolete)
987 B
987 B
Tips, tricks and best practices
Timeouts
To prevent a single task from taking too long, withTimeout can be used:
{{#include ../examples/timeoutsimple.nim}}
When several tasks should share a single timeout, a common timer can be created
with sleepAsync:
{{#include ../examples/timeoutcomposed.nim}}
discard
When calling an asynchronous procedure without await, the operation is started
but its result is not processed until corresponding Future is read.
It is therefore important to never discard futures directly - instead, one
can discard the result of awaiting the future or use asyncSpawn to monitor
the outcome of the future as if it were running in a separate thread.
Similar to threads, tasks managed by asyncSpawn may causes the application to
crash if any exceptions leak out of it - use
checked exceptions to avoid this
problem.
{{#include ../examples/discards.nim}}