Skip to content

Commit d882176

Browse files
committed
add setter to manually maintain the async state if needed
1 parent 8d59e40 commit d882176

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-async-hook",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Async hook",
55
"author": "Sébastien Lorber",
66
"license": "MIT",

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const normalizeOptions = <R>(
6060

6161
type UseAsyncStateResult<R> = {
6262
value: AsyncState<R>;
63+
set: (value: AsyncState<R>) => void;
6364
setLoading: () => void;
6465
setResult: (r: R) => void;
6566
setError: (e: Error) => void;
@@ -70,6 +71,7 @@ const useAsyncState = <R extends {}>(
7071
const [value, setValue] = useState<AsyncState<R>>(InitialAsyncState);
7172
return {
7273
value,
74+
set: setValue,
7375
setLoading: () => setValue(options.setLoading(value)),
7476
setResult: result => setValue(options.setResult(result, value)),
7577
setError: error => setValue(options.setError(error, value)),
@@ -102,6 +104,7 @@ const useCurrentPromise = <R>(): UseCurrentPromiseReturn<R> => {
102104
};
103105

104106
export type UseAsyncReturn<R> = AsyncState<R> & {
107+
set: (value: AsyncState<R>) => void;
105108
execute: () => Promise<R>;
106109
currentPromise: Promise<R> | null;
107110
};
@@ -154,6 +157,7 @@ export const useAsync = <R, Args extends any[]>(
154157

155158
return {
156159
...AsyncState.value,
160+
set: AsyncState.set,
157161
execute: executeAsyncOperation,
158162
currentPromise: CurrentPromise.get(),
159163
};

0 commit comments

Comments
 (0)