Skip to content

Commit defe411

Browse files
committed
allow to pass no-args async functions, given args can be provided with closure-style
We want to avoid this: const slotDetailsAsync = useAsync( async (argSlot, argIsAdmin, argShowAdminDetails) => { if (argIsAdmin && argShowAdminDetails) { return getAdminSlotDetails(argSlot); } }, [shootingSlot, isAdmin, showAdminDetails] as [ ShootingSlotDTO, boolean, boolean ], ); Instead: const slotDetailsAsync = useAsync( async () => { if (argIsAdmin && argShowAdminDetails) { return getAdminSlotDetails(argSlot); } }, [shootingSlot, isAdmin, showAdminDetails], );
1 parent d299626 commit defe411

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
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.1.0",
3+
"version": "3.2.0",
44
"description": "Async hook",
55
"author": "Sébastien Lorber",
66
"license": "MIT",

src/index.ts

Lines changed: 6 additions & 4 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>,
124+
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>),
125125
params: Args,
126126
options?: UseAsyncOptions<R>
127127
): UseAsyncReturn<R, Args> => {
@@ -183,7 +183,7 @@ const useAsyncInternal = <R, Args extends any[]>(
183183
};
184184

185185
export const useAsync = <R, Args extends any[]>(
186-
asyncFunction: (...args: Args) => Promise<R>,
186+
asyncFunction: ((...args: Args) => Promise<R>) | (() => Promise<R>),
187187
params: Args,
188188
options?: UseAsyncOptions<R>
189189
): UseAsyncReturn<R, Args> => useAsyncInternal(asyncFunction, params, options);
@@ -195,7 +195,9 @@ type AddArg<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((
195195
: never;
196196

197197
export const useAsyncAbortable = <R, Args extends any[]>(
198-
asyncFunction: (...args: AddArg<AbortSignal, Args>) => Promise<R>,
198+
asyncFunction:
199+
| ((...args: AddArg<AbortSignal, Args>) => Promise<R>)
200+
| ((abortSignal: AbortSignal) => MaybePromise<R>),
199201
params: Args,
200202
options?: UseAsyncOptions<R>
201203
): UseAsyncReturn<R, Args> => {
@@ -229,7 +231,7 @@ export const useAsyncAbortable = <R, Args extends any[]>(
229231
};
230232

231233
export const useAsyncCallback = <R, Args extends any[]>(
232-
asyncFunction: (...args: Args) => MaybePromise<R>
234+
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>)
233235
): UseAsyncReturn<R, Args> => {
234236
return useAsyncInternal(
235237
asyncFunction,

0 commit comments

Comments
 (0)