Skip to content

Commit b9b9def

Browse files
Cleanup
1 parent 5088f82 commit b9b9def

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

_blogposts/2025-09-01-let-unwrap.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getUserPromises(id) {
6464
Let's have a look at the generated JS from the initial example in comparison:
6565

6666
```js
67-
async function getUserNestedSwitches(id) {
67+
async function getUser(id) {
6868
let error = await fetchUser(id);
6969
if (error.TAG !== "Ok") {
7070
return {
@@ -95,6 +95,8 @@ async function getUserNestedSwitches(id) {
9595
}
9696
```
9797

98+
As you can see, there is no extra calls to the standard library, but it's a little verbose.
99+
98100
## Introducing `let?`
99101

100102
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:
105107
let getUser = async (id) => {
106108
let? Ok(user) = await fetchUser(id)
107109
let? Ok(decodedUser) = decodeUser(user)
108-
Console.log(`Got user ${decodedUser.name}!`)
109110
let? Ok() = await ensureUserActive(decodedUser)
110111
Ok(decodedUser)
111112
}
@@ -122,7 +123,6 @@ async function getUser(id) {
122123
return e$1;
123124
}
124125
let decodedUser = e$1._0;
125-
console.log(`Got user ` + decodedUser.name + `!`);
126126
let e$2 = await ensureUserActive(decodedUser);
127127
if (e$2.TAG === "Ok") {
128128
return {
@@ -137,7 +137,7 @@ async function getUser(id) {
137137

138138
</CodeTab>
139139

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.
141141

142142
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.
143143

0 commit comments

Comments
 (0)