Skip to content

Commit

Permalink
allow to pass no-args async functions, given args can be provided wit…
Browse files Browse the repository at this point in the history
…h 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],
  );
  • Loading branch information
slorber committed Jun 19, 2019
1 parent d299626 commit defe411
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-async-hook",
"version": "3.1.0",
"version": "3.2.0",
"description": "Async hook",
"author": "Sébastien Lorber",
"license": "MIT",
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export type UseAsyncReturn<
// Relaxed interface which accept both async and sync functions
// Accepting sync function is convenient for useAsyncCallback
const useAsyncInternal = <R, Args extends any[]>(
asyncFunction: (...args: Args) => MaybePromise<R>,
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>),
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args> => {
Expand Down Expand Up @@ -183,7 +183,7 @@ const useAsyncInternal = <R, Args extends any[]>(
};

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

export const useAsyncAbortable = <R, Args extends any[]>(
asyncFunction: (...args: AddArg<AbortSignal, Args>) => Promise<R>,
asyncFunction:
| ((...args: AddArg<AbortSignal, Args>) => Promise<R>)
| ((abortSignal: AbortSignal) => MaybePromise<R>),
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args> => {
Expand Down Expand Up @@ -229,7 +231,7 @@ export const useAsyncAbortable = <R, Args extends any[]>(
};

export const useAsyncCallback = <R, Args extends any[]>(
asyncFunction: (...args: Args) => MaybePromise<R>
asyncFunction: ((...args: Args) => MaybePromise<R>) | (() => MaybePromise<R>)
): UseAsyncReturn<R, Args> => {
return useAsyncInternal(
asyncFunction,
Expand Down

0 comments on commit defe411

Please sign in to comment.