nim-chronos/docs/src/tips.md
Jacek Sieka 24be151cf3
move docs to docs (#466)
* 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)
2023-11-15 09:06:37 +01:00

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}}