tjsandnativeasync.nim 600 B

12345678910111213141516171819202122232425262728293031
  1. discard """
  2. output: '''hi
  3. bye'''
  4. """
  5. import async, times
  6. when defined(js):
  7. proc sleepAsync(t: int): Future[void] =
  8. var promise = newPromise() do(resolve: proc()):
  9. {.emit: """
  10. setTimeout(function(){
  11. `resolve`();
  12. }, `t`);
  13. """.}
  14. result = promise
  15. else:
  16. from asyncdispatch import sleepAsync, waitFor
  17. proc foo() {.async.} =
  18. echo "hi"
  19. var s = epochTime()
  20. await sleepAsync(200)
  21. var e = epochTime()
  22. doAssert(e - s > 0.1)
  23. echo "bye"
  24. when defined(js):
  25. discard foo()
  26. else:
  27. waitFor foo()