> Is this a wrong link? because that doesn't look synchronous.
Ops sorry I had changed the link.
> One important thing to know is that you should never use waitFor in an async
> procedure, use await.
If I use await, the main thread doesn't stop until all the coroutines I create
inside the loop finishes
proc download(node: JsonNode; downPath, board, thread: string):
Future[void] {.async.} =
var
tim = node["tim"].getInt()
ext = node["ext"].getStr()
downloader = newAsyncHttpClient()
downFileName = fmt"{downPath}/{tim}{ext}"
echo("Downloading ", node.mediaLink(board, thread), " as ", downFileName,
"...")
var downloadFuture = downloader.downloadFile(node.mediaLink(board,
thread), downFileName)
downloadFuture.callback=
proc() =
echo("Downloading ", downFileName, " complete")
result = downloadFuture
proc main() {.async.} =
var
boardAndThread = parseBoardAndThread(getRawLink())
downPath = getCurrentDir() & "/" & getRawDownPath()
board = boardAndThread[0]
thread = boardAndThread[1]
if not downPath.existsDir():
echo("Local path not recognized. Creating directory: ", downPath)
createDir downPath
var
webClient = newAsyncHttpClient()
link = getAPILink(board, thread)
j: string = await webClient.getContent(link)
var
jsonArray = parseJson(j)["posts"].getElems()
filteredArray = jsonArray.filter(proc(it: JsonNode): bool = it{"tim"}
!= nil)
downloadSignaledArray = filteredArray.map(proc(it: JsonNode):
Future[void] = it.download(downPath, board, thread))
for it in downloadSignaledArray:
await it
waitFor main() # never gets to finish downloading.
Run
For context, this is how the json looks like:
[https://a.4cdn.org/po/thread/570368.json](https://a.4cdn.org/po/thread/570368.json)
I have to extract `tim` and `ext` from each item in the JArray. Then from the
program argument I have to parse `po` and build the download link like:
`https://i.4cdn.org/po/1546293948883.png`. I need to download from any JsonNode
that has `tim` asynchronously.