4

What would be a nim equivalent of this javascript program with setTimeout? Please don't hack with sleep(1000) and keep the code asynchronous.

setTimeout(
    function()
    {
        console.log("Hello world")
    },
    1000
)
user619271
  • 4,766
  • 5
  • 30
  • 35

1 Answers1

6

The asyncdispatch module defines a sleepAsync function: https://nim-lang.org/docs/asyncdispatch.html#sleepAsync,int

You can use that to create a setTimeout equivalent.

dom96
  • 1,012
  • 1
  • 9
  • 22
  • 2
    It is worth mentioning that setTimeout is not a language primitive, but rather a runtime primitive, provided by whichever runloop implementation you're using. E.g. if you're writing a command line tool (or a server), then Nim's asyncdispatch may be your choice and like @dom96 said, you can use sleepAsync. If you're writing a GUI app, the timers should be provided by your underlying platform SDK. – uran Sep 06 '17 at 16:05