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: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@
21
21
#### :rocket: New Feature
22
22
23
23
- Add `littleEndian` feature for `DataView` to Stdlib. https://github.com/rescript-lang/rescript/pull/7881
24
+
- Add `mapOkAsync`, `mapErrorAsync`, `flatMapOkAsync` and `flatMapErrorAsync` for async `result`'s to Stdlib. https://github.com/rescript-lang/rescript/pull/7906
`mapOkAsync(res, f)`: Asynchronously maps over the Ok value of a Result. When `res` is `Ok(n)`,
327
+
applies the async function `f` to `n` and wraps the result in `Ok`.
328
+
When `res` is `Error`, returns the error unchanged.
329
+
330
+
## Examples
331
+
332
+
```rescript
333
+
let asyncSquare = async x => x * x
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")
337
+
```
338
+
*/
339
+
letmapOkAsync: (
340
+
promise<result<'ok, 'error>>,
341
+
'ok=>'mappedOk,
342
+
) =>promise<result<'mappedOk, 'error>>
343
+
344
+
/**
345
+
`mapErrorAsync(res, f)`: Asynchronously maps over the Error value of a Result. When `res` is `Error(e)`,
346
+
applies the async function `f` to `e` and wraps the result in `Error`.
347
+
When `res` is `Ok`, returns the ok value unchanged.
348
+
349
+
## Examples
350
+
351
+
```rescript
352
+
let asyncFormatError = async e => `Error: ${e}`
353
+
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")
356
+
```
357
+
*/
358
+
letmapErrorAsync: (
359
+
promise<result<'ok, 'error>>,
360
+
'error=>'mappedError,
361
+
) =>promise<result<'ok, 'mappedError>>
362
+
363
+
/**
364
+
`flatMapOkAsync(res, f)`: Asynchronously flat-maps over the Ok value of a Result. When `res` is `Ok(n)`,
365
+
applies the async function `f` to `n` which returns a Promise of a Result.
366
+
When `res` is `Error`, returns the error unchanged.
367
+
368
+
## Examples
369
+
370
+
```rescript
371
+
let asyncValidate = async x =>
372
+
if x > 0 {
373
+
Ok(x * 2)
374
+
} else {
375
+
Error("Must be positive")
376
+
}
377
+
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")
380
+
```
381
+
*/
382
+
letflatMapOkAsync: (
383
+
promise<result<'ok, 'error>>,
384
+
'ok=>promise<result<'mappedOk, 'error>>,
385
+
) =>promise<result<'mappedOk, 'error>>
386
+
387
+
/**
388
+
`flatMapErrorAsync(res, f)`: Asynchronously flat-maps over the Error value of a Result. When `res` is `Error(e)`,
389
+
applies the async function `f` to `e` which returns a Promise of a Result.
390
+
When `res` is `Ok`, returns the ok value unchanged.
391
+
392
+
## Examples
393
+
394
+
```rescript
395
+
let asyncRecover = async error =>
396
+
if error === "timeout" {
397
+
Ok("default")
398
+
} else {
399
+
Error(error)
400
+
}
401
+
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