Skip to content

Commit 641d1d7

Browse files
committed
Almost reformat.
1 parent c34f927 commit 641d1d7

File tree

1 file changed

+84
-117
lines changed

1 file changed

+84
-117
lines changed

example-async/src/AA.res

+84-117
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@ let addTest1 = (t, x) => tests->Js.Array2.push((. ()) => t(. x))->ignore
1111

1212
let foo = async (. x, y) => x + y
1313

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+
}
2119

2220
let baz = async (. ()) => await bar(. foo)
2321

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+
}
3026

3127
testBaz->addTest
3228

@@ -42,13 +38,11 @@ let e3: testable = async (. ()) => await e1(.)
4238
let e4: testable = async (. ()) => await e2(.)
4339
let e5: testable = %raw(`function() { return Promise.reject(new Error('fail')) }`)
4440

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+
}
5246

5347
testTryCatch->addTest1(e1)
5448
testTryCatch->addTest1(e2)
@@ -62,13 +56,11 @@ testTryCatch->addTest1(e5)
6256

6357
let singlePromise = async (. x) => x + 1
6458

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+
}
7264

7365
//
7466
//
@@ -83,123 +75,102 @@ module Fetch = {
8375

8476
let explainError: unknown => string = %raw(`(e)=>e.toString()`)
8577

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)
9685
}
86+
}
9787

9888
testFetch->addTest1("https://www.google.com/sdkjdkghdsg")
9989
testFetch->addTest1("https://www.google.comsdkjdkghdsg")
10090

10191
//
10292
//
10393
// 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+
}
10997

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

113101
testWithCallback->addTest
114102

115103
//
116104
//
117105
// Async list
118106
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+
}
133116
}
134117

135118
let fetchAndCount = {
136119
let counter = ref(0)
137120

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+
}
145126

146127
ff
147128
}
148129

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+
}
166143
testFetchMany->addTest
167144

168145
//
169146
//
170147
// Fetch with Result type
171148
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)
179153
}
154+
}
180155
}
181156

182157
let nextFetch = (. _response) => Some("https://github.com/")
183158

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(_) => ()
199169
}
200-
| Error(_) => ()
201170
}
171+
| Error(_) => ()
202172
}
173+
}
203174

204175
// // imaginary syntax
205176
// let testFetchWithResult = async () =>
@@ -215,29 +186,25 @@ testFetchWithResult->addTest
215186
//
216187
// Run tests
217188

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])(.)
224192

225-
await
226-
runAllTests(. n + 1)
227-
}
193+
await runAllTests(. n + 1)
228194
}
195+
}
229196

230197
runAllTests(. 0)->ignore
231198

232199
//
233200
//
234201
// Curried functions
235202

236-
let bb = async x => await x
203+
let bb = async (x) => await x
237204

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

240-
let dd = async x => {y => (await x) + (await y)}
207+
let dd = async (x) => {y => (await x) + (await y)}
241208

242209
let ee = async (. x) => {y => (await x) + (await y)}
243210

0 commit comments

Comments
 (0)