File: async.ls

package info (click to toggle)
node-livescript 1.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 672 kB
  • sloc: makefile: 45
file content (218 lines) | stat: -rw-r--r-- 3,309 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Async functions
# -----------------
#
# * Async Function Definition

# async function should return a promise
do ->
  x = ->>
    5

  p = x!
  # Promise.resolve(p) === p iff x is a promise
  # source: http://www.ecma-international.org/ecma-262/6.0/#sec-promise.resolve
  eq Promise.resolve(p), p
  p.then ->
    eq it, 5

# async function as argument
do ->
  ok ->> 1

  # named async function
  ok <| :fn ->> 2


# async function definition
do ->
  x = ->>
    3

  y <- x!then

  eq y, 3

# async function with await
do ->
  x = ->>
    await Promise.resolve(2)

  y <- x!then

  eq y, 2

# async function with await Promise.all
do ->
  x = ->>
    await Promise.all [
      Promise.resolve(3)
      Promise.resolve(4)
    ]

  y <- x!then

  eq y.length, 2
  eq y[0], 3
  eq y[1], 4

# async function calling other async functions
do ->
  z = ->>
    5

  x = ->>
    2 + await z()

  y <- x!then

  eq y, 7

# async function with await in for loop
do ->
  z = (x) ->>
    Promise.resolve(x * 2)

  x = ->>
    output = 0
    for i from 2 to 5
      output += await z(i)
    output

  y <- x!then

  eq y, 28

# async function with await in list comprehension
do ->
  x = ->>
    [await Promise.resolve(i) for i from 2 til 5]

  y <- x!then

  eq y.length, 3
  eq y[0], 2
  eq y[1], 3
  eq y[2], 4

# async function awaiting on a callback-based function
do ->
  y = (callback) ->
    callback(6)

  x = ->>
    2 + await new Promise ->
      y(it)

  y <- x!then

  eq y, 8

# async functions with do
do ->
  y <- (do ->> 9).then

  eq y, 9

# nested async functions
do ->
  x = ->>
    2 + await do ->>
      6 + await do ->>
        5 + await do ->>
          3

  y <- x!then

  eq y, 16

# async function name(arglist) syntax
do ->
  async function x(n, m)
    5 + n + m

  y <- x(3, 4).then

  eq y, 12

# hushed async functions should still return Promises, but they shouldn't be implicitly fulfiled with the value of the body
do ->
  x = !->>
    5

  p = x!
  eq Promise.resolve(p), p

  p.then ->
    eq it, undefined

# curried async function
do ->
  x = (n, m) -->>
    5 + n + m

  y <- x(2)(3).then

  eq y, 10

# curried async function used with normal function call syntax
do ->
  x = (n, m) -->>
    5 + n + m

  y <- x(4, 6).then

  eq y, 15

# unbound async function
do ->
  obj = new
    @x = 3
    @unbound = ->>
      @x
  obj2 = x: 5
  obj2.unbound = obj.unbound
  y <- obj2.unbound!then
  eq y, 5

# bound async function
do ->
  obj = new
    @x = 3
    @bound = ~>>
      @x
  obj2 = x: 5
  obj2.bound = obj.bound
  y <- obj2.bound!then
  eq y, 3

# [LiveScript#1019](https://github.com/gkz/LiveScript/issues/1019)
# in `let` blocks
do ->
  x = ->>
    let a = Promise.resolve 1
      await a

  y <- x!then!
  eq y, 1

# [LiveScript#1021](https://github.com/gkz/LiveScript/issues/1021)
# in for..let loops
do ->
  x = ->>
    for let v in [Promise.resolve 1; Promise.resolve 2]
      await v

  y <- x!then!
  eq "#y" '1,2'

# [LiveScript#1023](https://github.com/gkz/LiveScript/issues/1023)
# Loop guards (`when`, `case`, `|`) didn't work with `for..let` loops with `yield` in their bodies
do ->
  x = (keys) ->>
    pr = Promise~resolve
    obj = {a: pr 1; b: pr 2; c: pr 3}
    for own let k, v of obj when k in keys
      await v

  y <- x(<[ a c ]>).then!
  eq "#y", '1,3'