Skip to content

Instantly share code, notes, and snippets.

@dom96
Last active April 27, 2016 12:57
Show Gist options
  • Save dom96/2f6fd3b887ac755776544ec4006b94d0 to your computer and use it in GitHub Desktop.
Save dom96/2f6fd3b887ac755776544ec4006b94d0 to your computer and use it in GitHub Desktop.
var respChan: Channel[(string, string)]
var handlers: Table[string, proc (response: string)]
proc httpLoop() {.async.} =
while true:
let (received, resp) = respChan.tryRecv()
if received:
handlers[resp[0]](resp[1])
await sleepAsync(1000)
proc doHttpRequest(url: string, handler: proc(response: string)) = # we can't modify the header of this proc
echo "will do the request in background"
handlers[url] = handler
# dispatch some async stuff in the background, which will somehow call the `handler`
# After this line we can't modify the code
routes:
get "/":
echo "will do request"
doHttpRequest("myurl", proc(r: string) = echo "got response")
echo "response handled"
resp Http200, "ok"
asyncCheck httpLoop
runForever()
# After the request to "/", we should see the following output
# will do request
# will do the request in background
# response handled
# ... some time passes
# got response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment