Last active
May 6, 2018 08:46
-
-
Save Yardanico/5ffda1e507ba024de63f26a1f060f274 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
import asyncdispatch | |
template scheduleCall(ms: int, oneshot: bool, call: untyped) = | |
# We use `block` here because we can't use gensym with async | |
block: | |
proc sched() {.async.} = | |
await sleepAsync(ms) | |
await call | |
if not oneshot: | |
# We don't use await here because otherwise every sched() call | |
# will be included in possible stack traces | |
asyncCheck sched() | |
asyncCheck sched() | |
proc mydata(data: string) {.async.} = | |
echo "Mydata called with " & data | |
proc main {.async.} = | |
echo "Scheduling it..." | |
scheduleCall(250, false, mydata("250")) | |
scheduleCall(500, false, mydata("500")) | |
scheduleCall(750, false, mydata("750")) | |
scheduleCall(1000, false, mydata("1000")) | |
scheduleCall(1250, false, mydata("1250")) | |
scheduleCall(1500, false, mydata("1500")) | |
echo "Not blocking!" | |
asyncCheck main() | |
runForever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment