File: async_iterators.coffee

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,360 kB
  • sloc: makefile: 20; xml: 9; sh: 6; javascript: 5
file content (32 lines) | stat: -rw-r--r-- 757 bytes parent folder | download | duplicates (2)
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
# This is always fulfilled.
winLater = (val, ms) ->
  new Promise (resolve) -> setTimeout (-> resolve val), ms

# This is always rejected.
failLater = (val, ms) ->
  new Promise (resolve, reject) -> setTimeout (-> reject new Error val), ms

createAsyncIterable = (syncIterable) ->
  for elem in syncIterable
    yield await winLater elem, 50

test "async iteration", ->
  foo = (x for await x from createAsyncIterable [1,2,3])
  arrayEq foo, [1, 2, 3]

test "async generator functions", ->
  foo = (val) ->
    yield await winLater val + 1, 50

  bar = (val) ->
    yield await failLater val - 1, 50

  a = await foo(41).next()
  eq a.value, 42

  try
    b = do -> await bar(41).next()
    b.catch (err) ->
      eq "40", err.message
  catch err
    ok no