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: _blogposts/2025-09-01-let-unwrap.mdx
+79-3Lines changed: 79 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ Two observations:
35
35
1. with every `switch` expression, this function gets nested deeper.
36
36
2. The `Error` branch of every `switch` is just an identity mapper (neither wrapper nor contents change).
37
37
38
-
The only alternative in ReScript was always to use some specialized methods:
38
+
The only alternative in ReScript was always to use some specialized functions:
39
39
40
40
```res
41
41
let getUserPromises = id =>
@@ -46,12 +46,60 @@ let getUserPromises = id =>
46
46
47
47
**Note**: `Result.flatMapOkAsync` among some other async result helper functions are brand new in ReScript 12 as well!
48
48
49
-
This is arguably better, more concise, but also harder to understand because we have two wrapper types here, `promise` and `result`. And we have to wrap the non-async type in a `Promise.resolve` in order to stay on the same type level. Also we are giving up on our precious `async`/`await` syntax here.
49
+
This is arguably better, more concise, but also harder to understand because we have two wrapper types here, `promise` and `result`. And we have to wrap the non-async type in a `Promise.resolve` in order to stay on the same type level. Also we are giving up on our precious `async`/`await` syntax here. Furthermore, those functions result in two more function calls.
This strikes a balance between conciseness and simplicity that we really think fits ReScript well.
66
140
67
141
With `let?`, we can now safely focus on the the happy-path in the scope of the function. There is no nesting as the `Error` is automatically mapped. But be assured the error case is also handled as the type checker will complain when you don't handle the `Error` returned by the `getUser` function.
68
142
69
-
This desugars to a **sequence** of early-returns that you’d otherwise write by hand, so there’s **no extra runtime cost** and it plays nicely with `async/await` as the example above suggests.
143
+
This desugars to a **sequence** of early-returns that you’d otherwise write by hand, so there’s **no extra runtime cost** and it plays nicely with `async/await` as the example above suggests. Check the `JS Output` tab to see for yourself!
70
144
71
145
Of course, it also works for `option` with `Some(...)`.
0 commit comments