This should work. Extending `Future` are more straightforward
# Background:
# - Test harness to check out how to extend a httpclient future to include
# additional context information (in this case the source url which could
be
# usefull in reporting failures)
import std/[asyncdispatch, httpclient, strutils]
type URLinfo = object
url: string
content: string
# Non-working attempt to implement the extended future
proc fetch(url: string): Future[URLinfo] {.async.} =
let
client = newAsyncHttpClient()
future = await client.getContent url
result = URLInfo(url: url, content: future)
echo "fetch finished for url: ", url
proc getURLs(urls: seq[string]) : Future[seq[URLinfo]] {.async.} =
var futures: seq[Future[URLinfo]]
for url in urls:
var future = fetch url
futures.add future
result = await all futures
echo "getURLs finished"
proc main() {.async} =
const sites = @[
"https://google.com",
"https://youtube.com",
"https://facebook.com",
"https://twitter.com",
"https://nim-lang.org",
"https://reddit.com",
"https://amazon.com"
]
discard await getURLs sites
waitFor main()
echo "main finished"
Run