tfuturevar.nim 1.0 KB

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