In my code I kept making calls to async functions that return future types from 
inside a loop. How can I prevent the main thread from finishing before all of 
those futures to finish? Note that it's inside a loop that creates are multiple 
futures that need finishing.
    
    
    proc main() {.async.}=
      if os.paramCount() < 2:
        quit("Argument required. Usage: $threadgrabber <Thread link> <Relative 
download directory>")
      else:
        var
          boardAndThread = parseBoardAndThread(os.paramStr(1))
          downPath = getCurrentDir() & "/" & os.paramStr(2)
          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)
        for node in parseJson(j)["posts"]:
          if node{"tim"} != nil:
            var
              tim = node["tim"].getInt()
              ext = node["ext"].getStr()
              downFileName = fmt"{downPath}/{tim}{ext}"
            echo("Downloading ", node.mediaLink(board, thread), " as ", 
downFileName, "...")
            var downloadFuture = webClient.downloadFile(node.mediaLink(board, 
thread), downFileName)
            downloadFuture.callback=
              proc() =
                echo("Downloading ", tim, ".", ext, " Complete")
    
    waitFor main()
    
    
    Run

Reply via email to