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
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ function getUserPromises(id) {
64
64
Let's have a look at the generated JS from the initial example in comparison:
65
65
66
66
```js
67
-
asyncfunctiongetUserNestedSwitches(id) {
67
+
asyncfunctiongetUser(id) {
68
68
let error =awaitfetchUser(id);
69
69
if (error.TAG!=="Ok") {
70
70
return {
@@ -95,6 +95,8 @@ async function getUserNestedSwitches(id) {
95
95
}
96
96
```
97
97
98
+
As you can see, there is no extra calls to the standard library, but it's a little verbose.
99
+
98
100
## Introducing `let?`
99
101
100
102
Let's rewrite the above example again with our new syntax:
@@ -105,7 +107,6 @@ Let's rewrite the above example again with our new syntax:
105
107
let getUser = async (id) => {
106
108
let? Ok(user) = await fetchUser(id)
107
109
let? Ok(decodedUser) = decodeUser(user)
108
-
Console.log(`Got user ${decodedUser.name}!`)
109
110
let? Ok() = await ensureUserActive(decodedUser)
110
111
Ok(decodedUser)
111
112
}
@@ -122,7 +123,6 @@ async function getUser(id) {
122
123
return e$1;
123
124
}
124
125
let decodedUser =e$1._0;
125
-
console.log(`Got user `+decodedUser.name+`!`);
126
126
let e$2 =awaitensureUserActive(decodedUser);
127
127
if (e$2.TAG==="Ok") {
128
128
return {
@@ -137,7 +137,7 @@ async function getUser(id) {
137
137
138
138
</CodeTab>
139
139
140
-
This strikes a balance between conciseness and simplicity that we really think fits ReScript well.
140
+
This strikes a balance between conciseness and simplicity that we really think fits ReScript well. Also the emitted JS is more concise than the initial example because we got rid of the manual error mapping.
141
141
142
142
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.
0 commit comments