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: pages/docs/manual/latest/exception.mdx
+7-7
Original file line number
Diff line number
Diff line change
@@ -134,7 +134,7 @@ throw {
134
134
135
135
## Catching JS Exceptions
136
136
137
-
To distinguish between JavaScript exceptions and ReScript exceptions, ReScript namespaces JS exceptions under the `Js.Exn.Error(payload)` variant. To catch an exception thrown from the JS side:
137
+
To distinguish between JavaScript exceptions and ReScript exceptions, ReScript namespaces JS exceptions under the `Exn.Error(payload)` variant. To catch an exception thrown from the JS side:
138
138
139
139
140
140
Throw an exception from JS:
@@ -158,25 +158,25 @@ try {
158
158
// call the external method
159
159
someJSFunctionThatThrows()
160
160
} catch {
161
-
| Js.Exn.Error(obj) =>
162
-
switch Js.Exn.message(obj) {
161
+
| Exn.Error(obj) =>
162
+
switch Exn.message(obj) {
163
163
| Some(m) => Console.log("Caught a JS exception! Message: " ++ m)
164
164
| None => ()
165
165
}
166
166
}
167
167
```
168
168
169
-
The `obj` here is of type `Js.Exn.t`, intentionally opaque to disallow illegal operations. To operate on `obj`, do like the code above by using the standard library's [`Js.Exn`](api/js/exn) module's helpers.
169
+
The `obj` here is of type `Exn.t`, intentionally opaque to disallow illegal operations. To operate on `obj`, do like the code above by using the standard library's [`Exn`](api/js/exn) module's helpers.
170
170
171
171
## Raise a JS Exception
172
172
173
-
`raise(MyException)` raises a ReScript exception. To raise a JavaScript exception (whatever your purpose is), use `Js.Exn.raiseError`:
173
+
`raise(MyException)` raises a ReScript exception. To raise a JavaScript exception (whatever your purpose is), use `Exn.raiseError`:
174
174
175
175
<CodeTablabels={["ReScript", "JS Output"]}>
176
176
177
177
```res example
178
178
let myTest = () => {
179
-
Js.Exn.raiseError("Hello!")
179
+
Exn.raiseError("Hello!")
180
180
}
181
181
```
182
182
```js
@@ -257,7 +257,7 @@ try {
257
257
} catch {
258
258
| Not_found => ... // catch a ReScript exception
259
259
| Invalid_argument(_) => ... // catch a second ReScript exception
260
-
| Js.Exn.Error(obj) => ... // catch the JS exception
@@ -130,7 +130,7 @@ Also notice `type_`. Since `type` is a reserved keyword in ReScript, you can use
130
130
Leveraging JavaScript's [dynamic `import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) to reduce bundle size and lazy load code as needed is easy in ReScript. It's also a little bit more convenient than in regular JavaScript because you don't need to keep track of file paths manually with ReScript's module system.
131
131
132
132
### Dynamically Importing Parts of a Module
133
-
Use the `Js.import` function to dynamically import a specific part of a module. Put whatever `let` binding you want to import in there, and you'll get a `promise` back resolving to that specific binding.
133
+
Use the `import` function to dynamically import a specific part of a module. Put whatever `let` binding you want to import in there, and you'll get a `promise` back resolving to that specific binding.
134
134
135
135
Let's look at an example. Imagine the following file `MathUtils.res`:
136
136
@@ -145,7 +145,7 @@ Now let's dynamically import the add function in another module, e.g. `App.res`:
145
145
```rescript
146
146
// App.res
147
147
let main = async () => {
148
-
let add = await Js.import(MathUtils.add)
148
+
let add = await import(MathUtils.add)
149
149
let onePlusOne = add(1, 1)
150
150
151
151
Console.log(onePlusOne)
@@ -164,7 +164,7 @@ async function main() {
164
164
</CodeTab>
165
165
166
166
### Dynamically Importing an Entire Module
167
-
The syntax for importing a whole module looks a little different, since we are operating on the module syntax level; instead of using `Js.import`, you may simply `await` the module itself:
167
+
The syntax for importing a whole module looks a little different, since we are operating on the module syntax level; instead of using `import`, you may simply `await` the module itself:
0 commit comments