tfuturestream.nim 807 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. file: "tfuturestream.nim"
  3. exitcode: 0
  4. output: '''
  5. 0
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. Done
  12. Finished
  13. '''
  14. """
  15. import asyncdispatch
  16. var fs = newFutureStream[int]()
  17. proc alpha() {.async.} =
  18. for i in 0 .. 5:
  19. await sleepAsync(1000)
  20. await fs.write(i)
  21. echo("Done")
  22. fs.complete()
  23. proc beta() {.async.} =
  24. while not fs.finished:
  25. let (hasValue, value) = await fs.read()
  26. if hasValue:
  27. echo(value)
  28. echo("Finished")
  29. asyncCheck alpha()
  30. waitFor beta()
  31. # TODO: Something like this should work eventually.
  32. # proc delta(): FutureStream[string] {.async.} =
  33. # for i in 0 .. 5:
  34. # await sleepAsync(1000)
  35. # result.put($i)
  36. # return ""
  37. # proc omega() {.async.} =
  38. # let fut = delta()
  39. # while not fut.finished():
  40. # echo(await fs.takeAsync())
  41. # echo("Finished")
  42. # waitFor omega()