You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/@rescript/runtime/Stdlib_Result.resi
+36-12Lines changed: 36 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -330,10 +330,16 @@ When `res` is `Error`, returns the error unchanged.
330
330
## Examples
331
331
332
332
```rescript
333
-
let asyncSquare = async x => x * x
333
+
let square = x => x * x
334
334
335
-
let result1 = Result.mapOkAsync(Promise.resolve(Ok(4)), asyncSquare) // Returns promise that resolves to Ok(16)
336
-
let result2 = Result.mapOkAsync(Promise.resolve(Error("invalid")), asyncSquare) // Returns promise that resolves to Error("invalid")
335
+
let result1 = await Result.mapOkAsync(Promise.resolve(Ok(4)), square)
336
+
result1 == Ok(16)
337
+
338
+
let result2 = await Result.mapOkAsync(
339
+
Promise.resolve(Error("invalid")),
340
+
square,
341
+
)
342
+
result2 == Error("invalid")
337
343
```
338
344
*/
339
345
letmapOkAsync: (
@@ -349,10 +355,13 @@ When `res` is `Ok`, returns the ok value unchanged.
349
355
## Examples
350
356
351
357
```rescript
352
-
let asyncFormatError = async e => `Error: ${e}`
358
+
let formatError = e => `Error: ${e}`
353
359
354
-
let result1 = Result.mapErrorAsync(Promise.resolve(Ok(42)), asyncFormatError) // Returns promise that resolves to Ok(42)
355
-
let result2 = Result.mapErrorAsync(Promise.resolve(Error("invalid")), asyncFormatError) // Returns promise that resolves to Error("Error: invalid")
360
+
let result1 = await Result.mapErrorAsync(Promise.resolve(Ok(42)), formatError)
361
+
result1 == Ok(42)
362
+
363
+
let result2 = await Result.mapErrorAsync(Promise.resolve(Error("invalid")), formatError)
364
+
result2 == Error("Error: invalid")
356
365
```
357
366
*/
358
367
letmapErrorAsync: (
@@ -368,15 +377,21 @@ When `res` is `Error`, returns the error unchanged.
368
377
## Examples
369
378
370
379
```rescript
371
-
let asyncValidate = async x =>
380
+
let asyncValidate = async x =>
372
381
if x > 0 {
373
382
Ok(x * 2)
374
383
} else {
375
384
Error("Must be positive")
376
385
}
377
386
378
-
let result1 = Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate) // Returns promise that resolves to Ok(10)
379
-
let result2 = Result.flatMapOkAsync(Promise.resolve(Error("Already failed")), asyncValidate) // Returns promise that resolves to Error("Already failed")
387
+
let result1 = await Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate)
388
+
result1 == Ok(10)
389
+
390
+
let result2 = await Result.flatMapOkAsync(
391
+
Promise.resolve(Error("Already failed")),
392
+
asyncValidate,
393
+
)
394
+
result2 == Error("Already failed")
380
395
```
381
396
*/
382
397
letflatMapOkAsync: (
@@ -392,15 +407,24 @@ When `res` is `Ok`, returns the ok value unchanged.
392
407
## Examples
393
408
394
409
```rescript
395
-
let asyncRecover = async error =>
410
+
let asyncRecover = async error =>
396
411
if error === "timeout" {
397
412
Ok("default")
398
413
} else {
399
414
Error(error)
400
415
}
401
416
402
-
let result1 = Result.flatMapErrorAsync(Promise.resolve(Error("timeout")), asyncRecover) // Returns promise that resolves to Ok("default")
403
-
let result2 = Result.flatMapErrorAsync(Promise.resolve(Ok("default")), asyncRecover) // Returns promise that resolves to Ok("default")
0 commit comments