Last active
April 27, 2016 12:57
-
-
Save dom96/2f6fd3b887ac755776544ec4006b94d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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