try-ok 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,9 +45,9 @@ try {
|
|
|
45
45
|
With `try-ok`, you handle errors explicitly as return values:
|
|
46
46
|
|
|
47
47
|
```ts
|
|
48
|
-
import {
|
|
48
|
+
import { tryOk } from "@sangun-kang/try-ok";
|
|
49
49
|
|
|
50
|
-
const result = await
|
|
50
|
+
const result = await tryOk(fetch("/api/user").then(r => r.json()));
|
|
51
51
|
|
|
52
52
|
// 1. Handle Error First (Type Guard)
|
|
53
53
|
if (result.isError) {
|
|
@@ -63,10 +63,10 @@ console.log(result.data);
|
|
|
63
63
|
`try-ok` works well inside React components, especially when calling an existing async function:
|
|
64
64
|
|
|
65
65
|
```tsx
|
|
66
|
-
import {
|
|
66
|
+
import { tryOk } from "@sangun-kang/try-ok";
|
|
67
67
|
|
|
68
68
|
export default async function Page() {
|
|
69
|
-
const result = await
|
|
69
|
+
const result = await tryOk(getData());
|
|
70
70
|
|
|
71
71
|
if (result.isError) {
|
|
72
72
|
return <div>Oops!</div>;
|
|
@@ -95,7 +95,7 @@ You can strictly type your errors if needed:
|
|
|
95
95
|
```ts
|
|
96
96
|
type ApiError = { status: number; message: string };
|
|
97
97
|
|
|
98
|
-
const result = await
|
|
98
|
+
const result = await tryOk<User, ApiError>(getUser());
|
|
99
99
|
|
|
100
100
|
if (result.isError) {
|
|
101
101
|
// TypeScript knows this is ApiError
|