File tree Expand file tree Collapse file tree 2 files changed +90
-0
lines changed Expand file tree Collapse file tree 2 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,55 @@ async function testFetchMany() {
193
193
194
194
tests . push ( testFetchMany ) ;
195
195
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
+
196
245
async function runAllTests ( n ) {
197
246
if ( n >= 0 && n < tests . length ) {
198
247
await Caml_array . get ( tests , n ) ( ) ;
@@ -246,6 +295,9 @@ export {
246
295
AsyncList ,
247
296
fetchAndCount ,
248
297
testFetchMany ,
298
+ FetchResult ,
299
+ nextFetch ,
300
+ testFetchWithResult ,
249
301
runAllTests ,
250
302
bb ,
251
303
cc ,
Original file line number Diff line number Diff line change @@ -155,6 +155,44 @@ let testFetchMany =
155
155
156
156
testFetchMany -> addTest
157
157
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
+
158
196
//
159
197
//
160
198
// Run tests
You can’t perform that action at this time.
0 commit comments