Skip to content

Commit ade7fe1

Browse files
committed
set types for fetch-callback
1 parent 850aab7 commit ade7fe1

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

packages/react-async/src/useAsync.tsx

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -281,32 +281,40 @@ const useAsyncFetch = <T extends {}>(
281281
init,
282282
isDefer,
283283
})
284-
const state = useAsync({
285-
...options,
286-
[fn]: useCallback(
287-
// TODO
288-
(arg1: any, arg2: any, arg3: any) => {
289-
const [override, signal] = arg3 ? [arg1[0], arg3.signal] : [undefined, arg2.signal]
290-
if (typeof override === "object" && "preventDefault" in override) {
291-
// Don't spread Events or SyntheticEvents
292-
return doFetch(input, {
284+
const promiseFn = useCallback(
285+
(_: AsyncOptions<T>, { signal }: AbortController) => {
286+
return doFetch(input, {
287+
signal,
288+
...init,
289+
})
290+
},
291+
[identity] // eslint-disable-line react-hooks/exhaustive-deps
292+
)
293+
const deferFn = useCallback(
294+
([override]: any[], _: AsyncOptions<T>, { signal }: AbortController) => {
295+
if (typeof override === "object" && "preventDefault" in override) {
296+
// Don't spread Events or SyntheticEvents
297+
return doFetch(input, {
298+
signal,
299+
...init,
300+
})
301+
}
302+
return typeof override === "function"
303+
? doFetch(input, {
304+
signal,
305+
...override(init),
306+
})
307+
: doFetch(input, {
293308
signal,
294309
...init,
310+
...override,
295311
})
296-
}
297-
return typeof override === "function"
298-
? doFetch(input, {
299-
signal,
300-
...override(init),
301-
})
302-
: doFetch(input, {
303-
signal,
304-
...init,
305-
...override,
306-
})
307-
},
308-
[identity] // eslint-disable-line react-hooks/exhaustive-deps
309-
),
312+
},
313+
[identity] // eslint-disable-line react-hooks/exhaustive-deps
314+
)
315+
const state = useAsync({
316+
...options,
317+
[fn]: isDefer ? deferFn : promiseFn,
310318
})
311319
useDebugValue(state, ({ counter, status }) => `[${counter}] ${status}`)
312320
return state

0 commit comments

Comments
 (0)