@@ -11,22 +11,18 @@ let addTest1 = (t, x) => tests->Js.Array2.push((. ()) => t(. x))->ignore
11
11
12
12
let foo = async (. x , y ) => x + y
13
13
14
- let bar =
15
- async
16
- (. ff ) => {
17
- let a = await ff (. 3 , 4 )
18
- let b = await foo (. 5 , 6 )
19
- a + b
20
- }
14
+ let bar = async (. ff ) => {
15
+ let a = await ff (. 3 , 4 )
16
+ let b = await foo (. 5 , 6 )
17
+ a + b
18
+ }
21
19
22
20
let baz = async (. ()) => await bar (. foo )
23
21
24
- let testBaz : testable =
25
- async
26
- (. ()) => {
27
- let n = await baz (.)
28
- Js .log2 ("baz returned" , n )
29
- }
22
+ let testBaz : testable = async (. ()) => {
23
+ let n = await baz (.)
24
+ Js .log2 ("baz returned" , n )
25
+ }
30
26
31
27
testBaz -> addTest
32
28
@@ -42,13 +38,11 @@ let e3: testable = async (. ()) => await e1(.)
42
38
let e4 : testable = async (. ()) => await e2 (.)
43
39
let e5 : testable = %raw (` function () { return Promise .reject (new Error (' fail' )) }` )
44
40
45
- let testTryCatch =
46
- async
47
- (. fn ) =>
48
- try {await fn (.)} catch {
49
- | E (n ) => Js .log2 ("testTryCatch: E" , n )
50
- | JsError (_ ) => Js .log ("testTryCatch: JsError" )
51
- }
41
+ let testTryCatch = async (. fn ) =>
42
+ try {await fn (.)} catch {
43
+ | E (n ) => Js .log2 ("testTryCatch: E" , n )
44
+ | JsError (_ ) => Js .log ("testTryCatch: JsError" )
45
+ }
52
46
53
47
testTryCatch -> addTest1 (e1 )
54
48
testTryCatch -> addTest1 (e2 )
@@ -62,13 +56,11 @@ testTryCatch->addTest1(e5)
62
56
63
57
let singlePromise = async (. x ) => x + 1
64
58
65
- let nestedPromise =
66
- async
67
- (. x ) => {
68
- let resolve = x => [Js .Promise .resolve (x )]
69
- let _result = singlePromise (. x + 1 )-> resolve
70
- 32
71
- }
59
+ let nestedPromise = async (. x ) => {
60
+ let resolve = x => [Js .Promise .resolve (x )]
61
+ let _result = singlePromise (. x + 1 )-> resolve
62
+ 32
63
+ }
72
64
73
65
//
74
66
//
@@ -83,123 +75,102 @@ module Fetch = {
83
75
84
76
let explainError : unknown => string = %raw (` (e )=> e .toString ()` )
85
77
86
- let testFetch =
87
- async
88
- (. url ) => {
89
- open Fetch
90
- switch {await fetch (url )} {
91
- | response =>
92
- let status = response -> status
93
- Js .log2 ("Fetch returned status:" , status )
94
- | exception JsError (e ) => Js .log2 ("Fetch returned an error:" , e -> explainError )
95
- }
78
+ let testFetch = async (. url ) => {
79
+ open Fetch
80
+ switch {await fetch (url )} {
81
+ | response =>
82
+ let status = response -> status
83
+ Js .log2 ("Fetch returned status:" , status )
84
+ | exception JsError (e ) => Js .log2 ("Fetch returned an error:" , e -> explainError )
96
85
}
86
+ }
97
87
98
88
testFetch -> addTest1 ("https://www.google.com/sdkjdkghdsg" )
99
89
testFetch -> addTest1 ("https://www.google.comsdkjdkghdsg" )
100
90
101
91
//
102
92
//
103
93
// Callbacks
104
- let withCallback =
105
- async
106
- (. ()) => {
107
- async (. x ) => await (x -> Js .Promise .resolve ) + 1
108
- }
94
+ let withCallback = async (. ()) => {
95
+ async (. x ) => await (x -> Js .Promise .resolve ) + 1
96
+ }
109
97
110
- let testWithCallback =
111
- async (. ()) => Js .log2 ("callback returned" , await (await withCallback (.))(. 3 ))
98
+ let testWithCallback = async (. ()) =>
99
+ Js .log2 ("callback returned" , await (await withCallback (.))(. 3 ))
112
100
113
101
testWithCallback -> addTest
114
102
115
103
//
116
104
//
117
105
// Async list
118
106
module AsyncList = {
119
- let map =
120
- async
121
- (. l , f ) => {
122
- let rec loop =
123
- async
124
- (. l , acc ) =>
125
- switch l {
126
- | list {} => acc
127
- | list {p , ... rest } => await loop (. rest , list {await p , ... acc })
128
- }
129
-
130
- await
131
- loop (. l -> Belt .List .mapReverse (x => f (. x )), list {})
132
- }
107
+ let map = async (. l , f ) => {
108
+ let rec loop = async (. l , acc ) =>
109
+ switch l {
110
+ | list {} => acc
111
+ | list {p , ... rest } => await loop (. rest , list {await p , ... acc })
112
+ }
113
+
114
+ await loop (. l -> Belt .List .mapReverse (x => f (. x )), list {})
115
+ }
133
116
}
134
117
135
118
let fetchAndCount = {
136
119
let counter = ref (0 )
137
120
138
- let ff =
139
- async
140
- (. url ) => {
141
- let response = await Fetch .fetch (url )
142
- counter := counter .contents + 1
143
- (counter .contents , response -> Fetch .status )
144
- }
121
+ let ff = async (. url ) => {
122
+ let response = await Fetch .fetch (url )
123
+ counter := counter .contents + 1
124
+ (counter .contents , response -> Fetch .status )
125
+ }
145
126
146
127
ff
147
128
}
148
129
149
- let testFetchMany =
150
- async
151
- (. ()) => {
152
- let fetchedItems =
153
- await
154
- AsyncList .map (.
155
- list {
156
- "https://www.google.com" ,
157
- "https://www.google.com" ,
158
- "https://www.google.com" ,
159
- "https://www.google.com" ,
160
- "https://www.google.com" ,
161
- },
162
- fetchAndCount ,
163
- )
164
- fetchedItems -> Belt .List .forEach (((i , s )) => Js .log3 ("Fetched" , i , s ))
165
- }
130
+ let testFetchMany = async (. ()) => {
131
+ let fetchedItems = await AsyncList .map (.
132
+ list {
133
+ "https://www.google.com" ,
134
+ "https://www.google.com" ,
135
+ "https://www.google.com" ,
136
+ "https://www.google.com" ,
137
+ "https://www.google.com" ,
138
+ },
139
+ fetchAndCount ,
140
+ )
141
+ fetchedItems -> Belt .List .forEach (((i , s )) => Js .log3 ("Fetched" , i , s ))
142
+ }
166
143
testFetchMany -> addTest
167
144
168
145
//
169
146
//
170
147
// Fetch with Result type
171
148
module FetchResult = {
172
- let fetch =
173
- async
174
- (. url ) => {
175
- switch {await Fetch .fetch (url )} {
176
- | response => Ok (response )
177
- | exception JsError (e ) => Error (e )
178
- }
149
+ let fetch = async (. url ) => {
150
+ switch {await Fetch .fetch (url )} {
151
+ | response => Ok (response )
152
+ | exception JsError (e ) => Error (e )
179
153
}
154
+ }
180
155
}
181
156
182
157
let nextFetch = (. _response ) => Some ("https://github.com/" )
183
158
184
- let testFetchWithResult =
185
- async
186
- (. ()) => {
187
- switch await
188
- FetchResult .fetch (. "https://www.google.com" ) {
189
- | Ok (response1 ) =>
190
- Js .log2 ("FetchResult response1" , response1 -> Fetch .status )
191
- switch nextFetch (. response1 ) {
192
- | None => ()
193
- | Some (url ) =>
194
- switch await
195
- FetchResult .fetch (. url ) {
196
- | Ok (response2 ) => Js .log2 ("FetchResult response2" , response2 -> Fetch .status )
197
- | Error (_ ) => ()
198
- }
159
+ let testFetchWithResult = async (. ()) => {
160
+ switch await FetchResult .fetch (. "https://www.google.com" ) {
161
+ | Ok (response1 ) =>
162
+ Js .log2 ("FetchResult response1" , response1 -> Fetch .status )
163
+ switch nextFetch (. response1 ) {
164
+ | None => ()
165
+ | Some (url ) =>
166
+ switch await FetchResult .fetch (. url ) {
167
+ | Ok (response2 ) => Js .log2 ("FetchResult response2" , response2 -> Fetch .status )
168
+ | Error (_ ) => ()
199
169
}
200
- | Error (_ ) => ()
201
170
}
171
+ | Error (_ ) => ()
202
172
}
173
+ }
203
174
204
175
// // imaginary syntax
205
176
// let testFetchWithResult = async () =>
@@ -215,29 +186,25 @@ testFetchWithResult->addTest
215
186
//
216
187
// Run tests
217
188
218
- let rec runAllTests =
219
- async
220
- (. n ) => {
221
- if n >= 0 && n < Array .length (tests ) {
222
- await
223
- (@doesNotRaise tests [n ])(.)
189
+ let rec runAllTests = async (. n ) => {
190
+ if n >= 0 && n < Array .length (tests ) {
191
+ await (@doesNotRaise tests [n ])(.)
224
192
225
- await
226
- runAllTests (. n + 1 )
227
- }
193
+ await runAllTests (. n + 1 )
228
194
}
195
+ }
229
196
230
197
runAllTests (. 0 )-> ignore
231
198
232
199
//
233
200
//
234
201
// Curried functions
235
202
236
- let bb = async x => await x
203
+ let bb = async ( x ) => await x
237
204
238
- let cc = async (x , ~y = x , z ) => (await x ) + ( await y ) + (await z )
205
+ let cc = async (x , ~y = x , z ) => (await x ) + await y + (await z )
239
206
240
- let dd = async x => {y => (await x ) + (await y )}
207
+ let dd = async ( x ) => {y => (await x ) + (await y )}
241
208
242
209
let ee = async (. x ) => {y => (await x ) + (await y )}
243
210
0 commit comments