Skip to content

Commit 976658e

Browse files
committed
Example with exception-annotated fetch.
1 parent f11da7a commit 976658e

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

example-async/bsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"name": "example-async",
3+
"reanalyze": {
4+
"analysis": ["exception"]
5+
},
36
"sources": [
47
{
58
"dir": "src",

example-async/src/AA.mjs

+18-4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ async function nestedPromise(x) {
102102
return 32;
103103
}
104104

105+
function $$fetch$1(url) {
106+
return fetch(url);
107+
}
108+
109+
function status(response) {
110+
return response.status;
111+
}
112+
113+
var Fetch = {
114+
$$fetch: $$fetch$1,
115+
status: status
116+
};
117+
105118
var explainError = ((e)=>e.toString());
106119

107120
async function testFetch(url) {
@@ -193,7 +206,7 @@ async function testFetchMany() {
193206

194207
tests.push(testFetchMany);
195208

196-
async function $$fetch$1(url) {
209+
async function $$fetch$2(url) {
197210
var response;
198211
try {
199212
response = await fetch(url);
@@ -215,15 +228,15 @@ async function $$fetch$1(url) {
215228
}
216229

217230
var FetchResult = {
218-
$$fetch: $$fetch$1
231+
$$fetch: $$fetch$2
219232
};
220233

221234
function nextFetch(_response) {
222235
return "https://github.com/";
223236
}
224237

225238
async function testFetchWithResult() {
226-
var response1 = await $$fetch$1("https://www.google.com");
239+
var response1 = await $$fetch$2("https://www.google.com");
227240
if (response1.TAG !== /* Ok */0) {
228241
return ;
229242
}
@@ -233,7 +246,7 @@ async function testFetchWithResult() {
233246
if (url === undefined) {
234247
return ;
235248
}
236-
var response2 = await $$fetch$1(url);
249+
var response2 = await $$fetch$2(url);
237250
if (response2.TAG !== /* Ok */0) {
238251
return ;
239252
}
@@ -288,6 +301,7 @@ export {
288301
testTryCatch ,
289302
singlePromise ,
290303
nestedPromise ,
304+
Fetch ,
291305
explainError ,
292306
testFetch ,
293307
withCallback ,

example-async/src/AA.res

+14-5
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,23 @@ let nestedPromise =
7474
//
7575
// Test error handling in fetch
7676

77+
module Fetch = {
78+
@raises(JsError)
79+
let fetch = url => Fetch.fetch(url)
80+
81+
@raises([])
82+
let status = response => Fetch.Response.status(response)
83+
}
84+
7785
let explainError: unknown => string = %raw(`(e)=>e.toString()`)
7886

7987
let testFetch =
8088
@async
8189
(. url) => {
82-
switch {@await Fetch.fetch(url)} {
90+
open Fetch
91+
switch {@await fetch(url)} {
8392
| response =>
84-
let status = response->Fetch.Response.status
93+
let status = response->status
8594
Js.log2("Fetch returned status:", status)
8695
| exception JsError(e) => Js.log2("Fetch returned an error:", e->explainError)
8796
}
@@ -132,7 +141,7 @@ let fetchAndCount = {
132141
(. url) => {
133142
let response = @await Fetch.fetch(url)
134143
counter := counter.contents + 1
135-
(counter.contents, response->Fetch.Response.status)
144+
(counter.contents, response->Fetch.status)
136145
}
137146

138147
ff
@@ -177,13 +186,13 @@ let testFetchWithResult =
177186
switch @await
178187
FetchResult.fetch(. "https://www.google.com") {
179188
| Ok(response1) =>
180-
Js.log2("FetchResult response1", response1->Fetch.Response.status)
189+
Js.log2("FetchResult response1", response1->Fetch.status)
181190
switch nextFetch(. response1) {
182191
| None => ()
183192
| Some(url) =>
184193
switch @await
185194
FetchResult.fetch(. url) {
186-
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.Response.status)
195+
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.status)
187196
| Error(_) => ()
188197
}
189198
}

0 commit comments

Comments
 (0)