Skip to content

Commit b2b82da

Browse files
committed
commit 3.2.2 code
1 parent 14c9f7c commit b2b82da

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
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": "3.2.1",
3+
"version": "3.2.2",
44
"description": "Async hook",
55
"author": "Sébastien Lorber",
66
"license": "MIT",

src/index.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export type UseAsyncReturn<
121121
// Relaxed interface which accept both async and sync functions
122122
// Accepting sync function is convenient for useAsyncCallback
123123
const useAsyncInternal = <R, Args extends any[]>(
124-
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>),
124+
asyncFunction: (...args: Args) => MaybePromise<R>,
125125
params: Args,
126126
options?: UseAsyncOptions<R>
127127
): UseAsyncReturn<R, Args> => {
@@ -182,11 +182,26 @@ const useAsyncInternal = <R, Args extends any[]>(
182182
};
183183
};
184184

185-
export const useAsync = <R, Args extends any[]>(
186-
asyncFunction: ((...args: Args) => Promise<R>) | (() => Promise<R>),
185+
// override to allow passing an async function with no args:
186+
// gives more user-freedom to create an inline async function
187+
export function useAsync<R, Args extends any[]>(
188+
asyncFunction: () => Promise<R>,
187189
params: Args,
188190
options?: UseAsyncOptions<R>
189-
): UseAsyncReturn<R, Args> => useAsyncInternal(asyncFunction, params, options);
191+
): UseAsyncReturn<R, Args>;
192+
export function useAsync<R, Args extends any[]>(
193+
asyncFunction: (...args: Args) => Promise<R>,
194+
params: Args,
195+
options?: UseAsyncOptions<R>
196+
): UseAsyncReturn<R, Args>;
197+
198+
export function useAsync<R, Args extends any[]>(
199+
asyncFunction: (...args: Args) => Promise<R>,
200+
params: Args,
201+
options?: UseAsyncOptions<R>
202+
): UseAsyncReturn<R, Args> {
203+
return useAsyncInternal(asyncFunction, params, options);
204+
}
190205

191206
type AddArg<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((
192207
...r: infer R
@@ -195,9 +210,7 @@ type AddArg<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((
195210
: never;
196211

197212
export const useAsyncAbortable = <R, Args extends any[]>(
198-
asyncFunction:
199-
| ((...args: AddArg<AbortSignal, Args>) => Promise<R>)
200-
| ((abortSignal: AbortSignal) => MaybePromise<R>),
213+
asyncFunction: (...args: AddArg<AbortSignal, Args>) => Promise<R>,
201214
params: Args,
202215
options?: UseAsyncOptions<R>
203216
): UseAsyncReturn<R, Args> => {
@@ -231,7 +244,7 @@ export const useAsyncAbortable = <R, Args extends any[]>(
231244
};
232245

233246
export const useAsyncCallback = <R, Args extends any[]>(
234-
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>)
247+
asyncFunction: (...args: Args) => MaybePromise<R>
235248
): UseAsyncReturn<R, Args> => {
236249
return useAsyncInternal(
237250
asyncFunction,

0 commit comments

Comments
 (0)