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
Check the JS Output tab: that `expensiveFilesRead`'s code isn't executed yet, even though you declared it! You can carry it around without fearing that it'll run the directory read.
38
-
39
36
**Note**: a lazy value is **not** a [shared data type](shared-data-types.md). Don't rely on its runtime representation in your JavaScript code.
40
37
41
38
## Execute The Lazy Computation
42
39
43
-
To actually run the lazy value's computation, use `Lazy.force` from the globally available`Lazy` module:
40
+
To actually run the lazy value's computation, use `Lazy.get` from the standard library`Lazy` module:
44
41
45
42
<CodeTablabels={["ReScript", "JS Output"]}>
46
43
47
44
```res example
48
45
// First call. The computation happens
49
-
Console.log(Lazy.force(expensiveFilesRead)) // logs "Reading dir" and the directory content
46
+
Console.log(Lazy.get(expensiveFilesRead)) // logs "Reading dir" and the directory content
50
47
51
48
// Second call. Will just return the already calculated result
52
-
Console.log(Lazy.force(expensiveFilesRead)) // logs the directory content
49
+
Console.log(Lazy.get(expensiveFilesRead)) // logs the directory content
The first time `Lazy.force` is called, the expensive computation happens and the result is **cached**. The second time, the cached value is directly used.
63
-
64
-
**You can't re-trigger the computation after the first `force` call**. Make sure you only use a lazy value with computations whose results don't change (e.g. an expensive server request whose response is always the same).
65
-
66
-
Instead of using `Lazy.force`, you can also use [pattern matching](pattern-matching-destructuring.md) to trigger the computation:
67
-
68
-
<CodeTablabels={["ReScript", "JS Output"]}>
52
+
console.log(Lazy.get(expensiveFilesRead));
69
53
70
-
```res example
71
-
switch expensiveFilesRead {
72
-
| lazy(result) => Console.log(result)
73
-
}
74
-
```
75
-
```js
76
-
var result =CamlinternalLazy.force(expensiveFilesRead);
54
+
console.log(Lazy.get(expensiveFilesRead));
77
55
```
78
56
79
57
</CodeTab>
80
58
81
-
Since pattern matching also works on a `let` binding, you can also do:
82
-
83
-
<CodeTablabels={["ReScript", "JS Output"]}>
84
-
85
-
```res example
86
-
let lazy(result) = expensiveFilesRead
87
-
Console.log(result)
88
-
```
89
-
```js
90
-
var result =CamlinternalLazy.force(expensiveFilesRead);
91
-
console.log(result);
92
-
```
59
+
The first time `Lazy.get` is called, the expensive computation happens and the result is **cached**. The second time, the cached value is directly used.
93
60
94
-
</CodeTab>
61
+
**You can't re-trigger the computation after the first `get` call**. Make sure you only use a lazy value with computations whose results don't change (e.g. an expensive server request whose response is always the same).
95
62
96
63
## Exception Handling
97
64
@@ -101,18 +68,18 @@ For completeness' sake, our files read example might raise an exception because
101
68
102
69
```res example
103
70
let result = try {
104
-
Lazy.force(expensiveFilesRead)
71
+
Lazy.get(expensiveFilesRead)
105
72
} catch {
106
73
| Not_found => [] // empty array of files
107
74
}
108
75
```
109
76
```js
110
-
var result;
77
+
let result;
111
78
112
79
try {
113
-
result =CamlinternalLazy.force(expensiveFilesRead);
80
+
result =Lazy.get(expensiveFilesRead);
114
81
} catch (raw_exn) {
115
-
var exn =Caml_js_exceptions.internalToOCamlException(raw_exn);
82
+
let exn =Primitive_exceptions.internalToException(raw_exn);
0 commit comments