Skip to content

Commit 09875bc

Browse files
committed
Example: switch async to surface syntax.
1 parent 651cc68 commit 09875bc

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

example-async/src/AA.res

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

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

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

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

2424
let testBaz: testable =
25-
@res.async
25+
async
2626
(. ()) => {
2727
let n = @res.await baz(.)
2828
Js.log2("baz returned", n)
@@ -36,14 +36,14 @@ testBaz->addTest
3636

3737
exception E(int)
3838

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

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

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

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

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

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

113113
testWithCallback->addTest
114114

@@ -117,10 +117,10 @@ testWithCallback->addTest
117117
// Async list
118118
module AsyncList = {
119119
let map =
120-
@res.async
120+
async
121121
(. l, f) => {
122122
let rec loop =
123-
@res.async
123+
async
124124
(. l, acc) =>
125125
switch l {
126126
| list{} => acc
@@ -136,7 +136,7 @@ let fetchAndCount = {
136136
let counter = ref(0)
137137

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

149149
let testFetchMany =
150-
@res.async
150+
async
151151
(. ()) => {
152152
let fetchedItems =
153153
@res.await
@@ -170,7 +170,7 @@ testFetchMany->addTest
170170
// Fetch with Result type
171171
module FetchResult = {
172172
let fetch =
173-
@res.async
173+
async
174174
(. url) => {
175175
switch {@res.await Fetch.fetch(url)} {
176176
| response => Ok(response)
@@ -182,7 +182,7 @@ module FetchResult = {
182182
let nextFetch = (. _response) => Some("https://github.com/")
183183

184184
let testFetchWithResult =
185-
@res.async
185+
async
186186
(. ()) => {
187187
switch @res.await
188188
FetchResult.fetch(. "https://www.google.com") {
@@ -216,7 +216,7 @@ testFetchWithResult->addTest
216216
// Run tests
217217

218218
let rec runAllTests =
219-
@res.async
219+
async
220220
(. n) => {
221221
if n >= 0 && n < Array.length(tests) {
222222
@res.await
@@ -233,23 +233,23 @@ runAllTests(. 0)->ignore
233233
//
234234
// Curried functions
235235

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

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

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

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

244244
//
245245
//
246246
// Errors
247247

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

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

0 commit comments

Comments
 (0)