Skip to content

Commit 966af4a

Browse files
committed
feat: use discriminated union in useAsync
1 parent 8d3dbd4 commit 966af4a

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

docs/useAsync.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const fn = () => new Promise((resolve) => {
1717
});
1818

1919
const Demo = () => {
20-
const {loading, value, error} = useAsync(fn);
20+
const state = useAsync(fn);
2121

2222
return (
2323
<div>
24-
{loading
25-
? <div>Loading...</div>
26-
: <div>Value: {value}</div>
24+
{state.loading?
25+
<div>Loading...</div>
26+
: state.error?
27+
<div>Error...</div>
28+
: <div>Value: {state.value}</div>
2729
}
2830
</div>
2931
);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@storybook/react": "^3.4.11",
4444
"react": "next",
4545
"react-dom": "next",
46-
"typescript": "^3.1.3",
46+
"typescript": "^3.2.2",
4747
"ts-node": "^7.0.1",
4848
"ts-loader": "3",
4949
"babel-core": "^6.26.3",

src/useAsync.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import {useState, useEffect, useCallback} from 'react';
22

3-
export interface AsyncState<T> {
4-
loading: boolean;
5-
error?: Error | any;
6-
value?: T;
3+
export type AsyncState<T> =
4+
| {
5+
loading: true;
76
}
7+
| {
8+
loading: false;
9+
error: Error;
10+
}
11+
| {
12+
loading: false;
13+
error?: undefined;
14+
value: T;
15+
};
816

917
const useAsync = <T>(fn: () => Promise<T>, args?) => {
1018
const [state, set] = useState<AsyncState<T>>({

0 commit comments

Comments
 (0)