Skip to content

Commit 87c3831

Browse files
committed
Add example fetch using result type.
1 parent b666731 commit 87c3831

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

example-async/src/AA.mjs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,55 @@ async function testFetchMany() {
193193

194194
tests.push(testFetchMany);
195195

196+
async function $$fetch$1(url) {
197+
var response;
198+
try {
199+
response = await fetch(url);
200+
}
201+
catch (raw_e){
202+
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
203+
if (e.RE_EXN_ID === "JsError") {
204+
return {
205+
TAG: /* Error */1,
206+
_0: e._1
207+
};
208+
}
209+
throw e;
210+
}
211+
return {
212+
TAG: /* Ok */0,
213+
_0: response
214+
};
215+
}
216+
217+
var FetchResult = {
218+
$$fetch: $$fetch$1
219+
};
220+
221+
function nextFetch(_response) {
222+
return "https://github.com/";
223+
}
224+
225+
async function testFetchWithResult() {
226+
var response1 = await $$fetch$1("https://www.google.com");
227+
if (response1.TAG !== /* Ok */0) {
228+
return ;
229+
}
230+
var response1$1 = response1._0;
231+
console.log("FetchResult response1", response1$1.status);
232+
var url = nextFetch(response1$1);
233+
if (url === undefined) {
234+
return ;
235+
}
236+
var response2 = await $$fetch$1(url);
237+
if (response2.TAG !== /* Ok */0) {
238+
return ;
239+
}
240+
console.log("FetchResult response2", response2._0.status);
241+
}
242+
243+
tests.push(testFetchWithResult);
244+
196245
async function runAllTests(n) {
197246
if (n >= 0 && n < tests.length) {
198247
await Caml_array.get(tests, n)();
@@ -246,6 +295,9 @@ export {
246295
AsyncList ,
247296
fetchAndCount ,
248297
testFetchMany ,
298+
FetchResult ,
299+
nextFetch ,
300+
testFetchWithResult ,
249301
runAllTests ,
250302
bb ,
251303
cc ,

example-async/src/AA.res

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,44 @@ let testFetchMany =
155155

156156
testFetchMany->addTest
157157

158+
//
159+
//
160+
// Fetch with Result type
161+
module FetchResult = {
162+
let fetch =
163+
@async
164+
(. url) => {
165+
switch {@await Fetch.fetch(url)} {
166+
| response => Ok(response)
167+
| exception JsError(e) => Error(e)
168+
}
169+
}
170+
}
171+
172+
let nextFetch = (. _response) => Some("https://github.com/")
173+
174+
let testFetchWithResult =
175+
@async
176+
(. ()) => {
177+
switch @await
178+
FetchResult.fetch(. "https://www.google.com") {
179+
| Ok(response1) =>
180+
Js.log2("FetchResult response1", response1->Fetch.Response.status)
181+
switch nextFetch(. response1) {
182+
| None => ()
183+
| Some(url) =>
184+
switch @await
185+
FetchResult.fetch(. url) {
186+
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.Response.status)
187+
| Error(_) => ()
188+
}
189+
}
190+
| Error(_) => ()
191+
}
192+
}
193+
194+
testFetchWithResult->addTest
195+
158196
//
159197
//
160198
// Run tests

0 commit comments

Comments
 (0)