Files
nim-chronos/examples/timeoutcomposed.nim
Constantine MolchanovandJacek Sieka d70a2e30e4 Docs: HTTP Server tutorial (#622)
* Docs: HTTP Server tutorial.

* Add docstrings for Fence types.

* Docs: Examples: Reorganize HTTP server examples to Nimble projects instead of standalone files.

* Docs: Add shiftinclude an admonish preprocessors.

* Docs: Add admonish css.

* Add API docs building for apps inside chronos.

* Docs: HTTP Server: Finalize chapter 1.

* Docs: HTTP Server: Finalize intro.

* Docs: summary: Fix path to async_procs.md.

* Docs: HTTP Server: Finalize chapter 2.

* Docs: HTTP Server: Finalize chapter 3.

* CI: doc: Install mdbook-shiftinclude.

* Docs: HTTP Server: Finalize chapter 4.

* Docs: HTTP Server: Finalize chapter 5.

* CI: Update required mdbook-open-on-gh version.

* Docs: HTTP Server: Fix incorrect info about HttpProcessCallback2 in Chapter 4.

* Docs: http server: chapter 3,4: Replace mutating var with in-place try-except.

* Docs: http server: chapter 3,4: Remove explicit returns.

* Docs: http server: Use valueOr instead of explicit isErr checks.

* Docs: http server: Chapter 4: Add more info on middlewares.

* Docs: http server: Add info about HTTP protocol.

* Update docs/examples/http_server/chapter2/src/dashboard.nim

Co-authored-by: Jacek Sieka <jacek@status.im>

* Docs: HTTP Server: Remove redundant `return`s.

* Add examples tech files to ignore.

* Docs: Remove redundant css ref.

* Docs: HTTP Server: Replace global threadvar with a TableRef passed to a closure.

* Move examples out of docs.

Fix #662.

* Add example deps files to ignore.

---------

Co-authored-by: Jacek Sieka <jacek@status.im>
2026-06-23 15:15:56 +04:00

26 lines
714 B
Nim

## Single timeout for several operations
import chronos
proc shortTask() {.async.} =
try:
await sleepAsync(1.seconds)
except CancelledError as exc:
echo "Short task was cancelled!"
raise exc # Propagate cancellation to the next operation
proc composedTimeout() {.async.} =
let
# Common timout for several sub-tasks
timeout = sleepAsync(10.seconds)
while not timeout.finished():
let task = shortTask() # Start a task but don't `await` it
if (await race(task, timeout)) == task:
echo "Ran one more task"
else:
# This cancellation may or may not happen as task might have finished
# right at the timeout!
task.cancelSoon()
waitFor composedTimeout()