summary refs log blame commit diff stats
path: root/tests/async/tfuturestream.nim
blob: 9a8e986a09c4f8b3918f9f67e3cfb417296e33b3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                           

                    
                               


                        
                          
                     
 
              
               

                       
                        
                                           

                 



                  

              















                                                   
discard """
  file: "tfuturestream.nim"
  exitcode: 0
  output: '''
0
1
2
3
4
5
Done
Finished
'''
"""
import asyncdispatch

var fs = newFutureStream[int]()

proc alpha() {.async.} =
  for i in 0 .. 5:
    await sleepAsync(1000)
    await fs.write(i)

  echo("Done")
  fs.complete()

proc beta() {.async.} =
  while not fs.finished:
    let (hasValue, value) = await fs.read()
    if hasValue:
      echo(value)

  echo("Finished")

asyncCheck alpha()
waitFor beta()

# TODO: Something like this should work eventually.
# proc delta(): FutureStream[string] {.async.} =
#   for i in 0 .. 5:
#     await sleepAsync(1000)
#     result.put($i)

#   return ""

# proc omega() {.async.} =
#   let fut = delta()
#   while not fut.finished():
#     echo(await fs.takeAsync())

#   echo("Finished")

# waitFor omega()