Skip to content

Instantly share code, notes, and snippets.

@Yardanico
Last active May 6, 2018 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yardanico/5ffda1e507ba024de63f26a1f060f274 to your computer and use it in GitHub Desktop.
Save Yardanico/5ffda1e507ba024de63f26a1f060f274 to your computer and use it in GitHub Desktop.
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