Skip to content

Commit 7c30336

Browse files
WIP: Support preludes in playground links
1 parent d2f3ea4 commit 7c30336

File tree

11 files changed

+130
-525
lines changed

11 files changed

+130
-525
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ Before showing off this new feauture, let's explore why it is useful. Consider a
1616

1717
**Note**: While we are cautiously optimistic with this implementation of `let?`, we still consider it experimental and thus hide it behind a compiler flag that the user explicitly needs to activate. It might change so use at your own risk.
1818

19+
```res prelude
20+
type user = { id: string, name: string, token: string}
21+
external fetchUser: string => promise<result<JSON.t, [> #NetworkError | #UserNotFound | #Unauthorized]>> = "fetchUser"
22+
external decodeUser: JSON.t => result<user, [> #DecodeError]> = "decodeUser"
23+
external ensureUserActive: user => promise<result<unit, [> #UserNotActive]>> = "ensureUserActive"
24+
```
25+
1926
```res
2027
let getUser = async id =>
2128
switch await fetchUser(id) {
@@ -40,7 +47,7 @@ Two observations:
4047
The only alternative in ReScript was always to use some specialized functions:
4148

4249
```res
43-
let getUserPromises = id =>
50+
let getUser = id =>
4451
fetchUser(id)
4552
->Result.flatMapOkAsync(user => Promise.resolve(user->decodeUser))
4653
->Result.flatMapOkAsync(decodedUser => ensureUserActive(decodedUser))
@@ -51,7 +58,7 @@ let getUserPromises = id =>
5158
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.
5259

5360
```js
54-
function getUserPromises(id) {
61+
function getUser(id) {
5562
return Stdlib_Result.flatMapOkAsync(
5663
Stdlib_Result.flatMapOkAsync(fetchUser(id), (user) =>
5764
Promise.resolve(decodeUser(user)),
@@ -101,7 +108,7 @@ As you can see, there is no extra calls to the standard library, but it's a litt
101108

102109
Let's rewrite the above example again with our new syntax:
103110

104-
<CodeTab labels={["ReScript", "JS Output"]} experiments="LetUnwrap">
111+
<CodeTab labels={["ReScript", "JS Output"]} experiments="LetUnwrap" version="12.0.0-rc.1">
105112

106113
```rescript
107114
let getUser = async (id) => {
@@ -177,7 +184,7 @@ let? Ok(user) = await fetchUser("1")
177184

178185
### A full example with error handling
179186

180-
<CodeTab labels={["ReScript", "JS Output"]} experiments="LetUnwrap">
187+
<CodeTab labels={["ReScript", "JS Output"]} experiments="LetUnwrap" version="12.0.0-rc.1">
181188

182189
```rescript
183190
type user = {

0 commit comments

Comments
 (0)