Here is what you need to do to replicate your spawn example:
    
    
    # async version
    import httpclient, asyncdispatch, json , times
    let time = cpuTime()
    
    proc callStrategy (i : int)  {.async.}  =
      echo("request " & $i)
      let client = newAsyncHttpClient()
      client.headers = newHttpHeaders({ "Content-Type": "application/json" })
      let body = %*{
        "Pid":i
        }
      let response = await client.request("https://reqres.in/api/strategy";,
      httpMethod = HttpPost,
      body = $body)
      echo response.status
      echo await response.body
      echo("done " & $i)
    
    proc loop (num: int) {.async.} =
      echo "executing call number: " , num
      var futures: seq[Future[void]] = @[]
      for i in  1..num:
        echo "executing call number: " , i
        futures.add callStrategy(i)
      await all(futures)
    
    waitFor loop(10)
    echo "Time taken: ",cpuTime() - time
    
    
    
    Run

Reply via email to