summary refs log tree commit diff stats
path: root/tests/async/tfuturestream.nim
blob: d76752b7ea629dd841f96250841a8cc9f1b277de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 fs.write(i)
    await sleepAsync(1000)

  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()