mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-01-31 21:44:57 +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)
22 lines
663 B
Nim
22 lines
663 B
Nim
## Simple cancellation example
|
|
|
|
import chronos
|
|
|
|
proc someTask() {.async.} = await sleepAsync(10.minutes)
|
|
|
|
proc cancellationExample() {.async.} =
|
|
# Start a task but don't wait for it to finish
|
|
let future = someTask()
|
|
future.cancelSoon()
|
|
# `cancelSoon` schedules but does not wait for the future to get cancelled -
|
|
# it might still be pending here
|
|
|
|
let future2 = someTask() # Start another task concurrently
|
|
await future2.cancelAndWait()
|
|
# Using `cancelAndWait`, we can be sure that `future2` is either
|
|
# complete, failed or cancelled at this point. `future` could still be
|
|
# pending!
|
|
assert future2.finished()
|
|
|
|
waitFor(cancellationExample())
|