mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-01-21 00:39:32 +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)
25 lines
703 B
Nim
25 lines
703 B
Nim
## Make two http requests concurrently and output the one that wins
|
|
|
|
import chronos
|
|
import ./httpget
|
|
|
|
proc twoGets() {.async.} =
|
|
let
|
|
futs = @[
|
|
# Both pages will start downloading concurrently...
|
|
httpget.retrievePage("https://duckduckgo.com/?q=chronos"),
|
|
httpget.retrievePage("https://www.google.fr/search?q=chronos")
|
|
]
|
|
|
|
# Wait for at least one request to finish..
|
|
let winner = await one(futs)
|
|
# ..and cancel the others since we won't need them
|
|
for fut in futs:
|
|
# Trying to cancel an already-finished future is harmless
|
|
fut.cancelSoon()
|
|
|
|
# An exception could be raised here if the winning request failed!
|
|
echo "Result: ", winner.read()
|
|
|
|
waitFor(twoGets())
|