mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-01-10 03:15:55 +00:00
24be151cf3
* 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)
21 lines
504 B
Nim
21 lines
504 B
Nim
## Simple timeouts
|
|
import chronos
|
|
|
|
proc longTask {.async.} =
|
|
try:
|
|
await sleepAsync(10.minutes)
|
|
except CancelledError as exc:
|
|
echo "Long task was cancelled!"
|
|
raise exc # Propagate cancellation to the next operation
|
|
|
|
proc simpleTimeout() {.async.} =
|
|
let
|
|
task = longTask() # Start a task but don't `await` it
|
|
|
|
if not await task.withTimeout(1.seconds):
|
|
echo "Timeout reached - withTimeout should have cancelled the task"
|
|
else:
|
|
echo "Task completed"
|
|
|
|
waitFor simpleTimeout()
|