tfuturevar.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. discard """
  2. action: compile
  3. """
  4. # XXX: action should be run!
  5. import asyncdispatch
  6. proc completeOnReturn(fut: FutureVar[string], x: bool) {.async.} =
  7. if x:
  8. fut.mget() = ""
  9. fut.mget.add("foobar")
  10. return
  11. proc completeOnImplicitReturn(fut: FutureVar[string], x: bool) {.async.} =
  12. if x:
  13. fut.mget() = ""
  14. fut.mget.add("foobar")
  15. proc failureTest(fut: FutureVar[string], x: bool) {.async.} =
  16. if x:
  17. raise newException(Exception, "Test")
  18. proc manualComplete(fut: FutureVar[string], x: bool) {.async.} =
  19. if x:
  20. fut.mget() = "Hello World"
  21. fut.complete()
  22. return
  23. proc main() {.async.} =
  24. var fut: FutureVar[string]
  25. fut = newFutureVar[string]()
  26. await completeOnReturn(fut, true)
  27. doAssert(fut.read() == "foobar")
  28. fut = newFutureVar[string]()
  29. await completeOnImplicitReturn(fut, true)
  30. doAssert(fut.read() == "foobar")
  31. fut = newFutureVar[string]()
  32. let retFut = failureTest(fut, true)
  33. yield retFut
  34. doAssert(fut.read().len == 0)
  35. doAssert(fut.finished)
  36. fut = newFutureVar[string]()
  37. await manualComplete(fut, true)
  38. doAssert(fut.read() == "Hello World")
  39. waitFor main()