Skip to content

Commit 651cc68

Browse files
committed
Example: switch to the new res.* attributes.
1 parent ed7886b commit 651cc68

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

example-async/src/AA.res

+43-43
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ let addTest1 = (t, x) => tests->Js.Array2.push((. ()) => t(. x))->ignore
99
//
1010
// Basic tests
1111

12-
let foo = @async (. x, y) => x + y
12+
let foo = @res.async (. x, y) => x + y
1313

1414
let bar =
15-
@async
15+
@res.async
1616
(. ff) => {
17-
let a = @await ff(. 3, 4)
18-
let b = @await foo(. 5, 6)
17+
let a = @res.await ff(. 3, 4)
18+
let b = @res.await foo(. 5, 6)
1919
a + b
2020
}
2121

22-
let baz = @async (. ()) => @await bar(. foo)
22+
let baz = @res.async (. ()) => @res.await bar(. foo)
2323

2424
let testBaz: testable =
25-
@async
25+
@res.async
2626
(. ()) => {
27-
let n = @await baz(.)
27+
let n = @res.await baz(.)
2828
Js.log2("baz returned", n)
2929
}
3030

@@ -36,16 +36,16 @@ testBaz->addTest
3636

3737
exception E(int)
3838

39-
let e1: testable = @async (. ()) => raise(E(1000))
40-
let e2: testable = @async (. ()) => Js.Exn.raiseError("Some JS error")
41-
let e3: testable = @async (. ()) => @await e1(.)
42-
let e4: testable = @async (. ()) => @await e2(.)
39+
let e1: testable = @res.async (. ()) => raise(E(1000))
40+
let e2: testable = @res.async (. ()) => Js.Exn.raiseError("Some JS error")
41+
let e3: testable = @res.async (. ()) => @res.await e1(.)
42+
let e4: testable = @res.async (. ()) => @res.await e2(.)
4343
let e5: testable = %raw(`function() { return Promise.reject(new Error('fail')) }`)
4444

4545
let testTryCatch =
46-
@async
46+
@res.async
4747
(. fn) =>
48-
try {@await fn(.)} catch {
48+
try {@res.await fn(.)} catch {
4949
| E(n) => Js.log2("testTryCatch: E", n)
5050
| JsError(_) => Js.log("testTryCatch: JsError")
5151
}
@@ -60,10 +60,10 @@ testTryCatch->addTest1(e5)
6060
//
6161
// Check for nested promise
6262

63-
let singlePromise = @async (. x) => x + 1
63+
let singlePromise = @res.async (. x) => x + 1
6464

6565
let nestedPromise =
66-
@async
66+
@res.async
6767
(. x) => {
6868
let resolve = x => [Js.Promise.resolve(x)]
6969
let _result = singlePromise(. x + 1)->resolve
@@ -84,10 +84,10 @@ module Fetch = {
8484
let explainError: unknown => string = %raw(`(e)=>e.toString()`)
8585

8686
let testFetch =
87-
@async
87+
@res.async
8888
(. url) => {
8989
open Fetch
90-
switch {@await fetch(url)} {
90+
switch {@res.await fetch(url)} {
9191
| response =>
9292
let status = response->status
9393
Js.log2("Fetch returned status:", status)
@@ -102,13 +102,13 @@ testFetch->addTest1("https://www.google.comsdkjdkghdsg")
102102
//
103103
// Callbacks
104104
let withCallback =
105-
@async
105+
@res.async
106106
(. ()) => {
107-
@async (. x) => @await (x->Js.Promise.resolve) + 1
107+
@res.async (. x) => @res.await (x->Js.Promise.resolve) + 1
108108
}
109109

110110
let testWithCallback =
111-
@async (. ()) => Js.log2("callback returned", @await (@await withCallback(.))(. 3))
111+
@res.async (. ()) => Js.log2("callback returned", @res.await (@res.await withCallback(.))(. 3))
112112

113113
testWithCallback->addTest
114114

@@ -117,17 +117,17 @@ testWithCallback->addTest
117117
// Async list
118118
module AsyncList = {
119119
let map =
120-
@async
120+
@res.async
121121
(. l, f) => {
122122
let rec loop =
123-
@async
123+
@res.async
124124
(. l, acc) =>
125125
switch l {
126126
| list{} => acc
127-
| list{p, ...rest} => @await loop(. rest, list{@await p, ...acc})
127+
| list{p, ...rest} => @res.await loop(. rest, list{@res.await p, ...acc})
128128
}
129129

130-
@await
130+
@res.await
131131
loop(. l->Belt.List.mapReverse(x => f(. x)), list{})
132132
}
133133
}
@@ -136,9 +136,9 @@ let fetchAndCount = {
136136
let counter = ref(0)
137137

138138
let ff =
139-
@async
139+
@res.async
140140
(. url) => {
141-
let response = @await Fetch.fetch(url)
141+
let response = @res.await Fetch.fetch(url)
142142
counter := counter.contents + 1
143143
(counter.contents, response->Fetch.status)
144144
}
@@ -147,10 +147,10 @@ let fetchAndCount = {
147147
}
148148

149149
let testFetchMany =
150-
@async
150+
@res.async
151151
(. ()) => {
152152
let fetchedItems =
153-
@await
153+
@res.await
154154
AsyncList.map(.
155155
list{
156156
"https://www.google.com",
@@ -170,9 +170,9 @@ testFetchMany->addTest
170170
// Fetch with Result type
171171
module FetchResult = {
172172
let fetch =
173-
@async
173+
@res.async
174174
(. url) => {
175-
switch {@await Fetch.fetch(url)} {
175+
switch {@res.await Fetch.fetch(url)} {
176176
| response => Ok(response)
177177
| exception JsError(e) => Error(e)
178178
}
@@ -182,16 +182,16 @@ module FetchResult = {
182182
let nextFetch = (. _response) => Some("https://github.com/")
183183

184184
let testFetchWithResult =
185-
@async
185+
@res.async
186186
(. ()) => {
187-
switch @await
187+
switch @res.await
188188
FetchResult.fetch(. "https://www.google.com") {
189189
| Ok(response1) =>
190190
Js.log2("FetchResult response1", response1->Fetch.status)
191191
switch nextFetch(. response1) {
192192
| None => ()
193193
| Some(url) =>
194-
switch @await
194+
switch @res.await
195195
FetchResult.fetch(. url) {
196196
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.status)
197197
| Error(_) => ()
@@ -216,13 +216,13 @@ testFetchWithResult->addTest
216216
// Run tests
217217

218218
let rec runAllTests =
219-
@async
219+
@res.async
220220
(. n) => {
221221
if n >= 0 && n < Array.length(tests) {
222-
@await
222+
@res.await
223223
(@doesNotRaise tests[n])(.)
224224

225-
@await
225+
@res.await
226226
runAllTests(. n + 1)
227227
}
228228
}
@@ -233,23 +233,23 @@ runAllTests(. 0)->ignore
233233
//
234234
// Curried functions
235235

236-
let bb = @async x => @await x
236+
let bb = @res.async x => @res.await x
237237

238-
let cc = @async (x, ~y=x, z) => @await x + @await y + @await z
238+
let cc = @res.async (x, ~y=x, z) => (@res.await x) + (@res.await y) + (@res.await z)
239239

240-
let dd = @async x => {y => @await x + @await y}
240+
let dd = @res.async x => {y => (@res.await x) + (@res.await y)}
241241

242-
let ee = @async (. x) => {y => @await x + @await y}
242+
let ee = @res.async (. x) => {y => (@res.await x) + (@res.await y)}
243243

244244
//
245245
//
246246
// Errors
247247

248248
// let aa =
249-
// @async
249+
// @res.async
250250
// (. x) => {
251-
// let cb = (. _) => @await x // Error: Await on expression not in an async context
251+
// let cb = (. _) => @res.await x // Error: Await on expression not in an async context
252252
// cb
253253
// }
254254

255-
// let _ = @async (_, . x) => @await x // Error: Await on expression not in an async context
255+
// let _ = @res.async (_, . x) => @res.await x // Error: Await on expression not in an async context

0 commit comments

Comments
 (0)