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: docs/getting-started.md
+7-8Lines changed: 7 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -152,11 +152,10 @@ function readConfig(filePath: string) {
152
152
153
153
const result =readConfig("config.json");
154
154
// ^?
155
-
156
-
157
-
//
158
155
```
159
156
157
+
<divclass="spacer" />
158
+
160
159
If you look at the final `result`, you'll see exactly what the outcome of the function can be. And while technically the above code is correct, it is very verbose. Luckily, the library provides a way to make this code more concise.
161
160
162
161
### Adding the `Result.try` helper
@@ -194,15 +193,15 @@ function readConfig(filePath: string) {
194
193
() =>fs.readFileSync(filePath, "utf-8"),
195
194
(error) =>newIOError(`Unable to read file: ${filePath}`, { cause: error })
196
195
);
197
-
if (contentResult.isError()) {
196
+
if (!contentResult.ok) {
198
197
returncontentResult;
199
198
}
200
199
201
200
const jsonResult =Result.try(
202
201
() =>JSON.parse(contentResult.value),
203
202
() =>newParseError(`Unable to parse JSON from file: ${filePath}`)
0 commit comments