Skip to content

Commit 6340b08

Browse files
committed
Avoid nested ternaries.
1 parent 0663f5f commit 6340b08

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

packages/react-async/src/useAsync.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,16 @@ const useAsyncFetch = (input, init, { defer, json, ...options } = {}) => {
168168
const isDefer =
169169
typeof defer === "boolean" ? defer : ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
170170
const fn = isDefer ? "deferFn" : "promiseFn"
171-
const identity = JSON.stringify({ input, init, fn })
171+
const identity = JSON.stringify({ input, init, isDefer })
172172
const state = useAsync({
173173
...options,
174174
[fn]: useCallback(
175-
isDefer
176-
? ([override], _, { signal }) =>
177-
doFetch(input, {
178-
signal,
179-
...// override is a function, call it with init
180-
(typeof override === "function"
181-
? override(init)
182-
: // override is an Event or SyntheticEvent - do not spread
183-
"preventDefault" in override
184-
? init
185-
: // otherwise, spread override over init
186-
{ ...init, ...override }),
187-
})
188-
: (_, { signal }) => doFetch(input, { signal, ...init }),
175+
(arg1, arg2, arg3) => {
176+
const [override, signal] = isDefer ? [arg1[0], arg3.signal] : [undefined, arg2.signal]
177+
if (typeof override === "function") return doFetch(input, { signal, ...override(init) })
178+
if ("preventDefault" in override) return doFetch(input, { signal, ...init })
179+
return doFetch(input, { signal, ...init, ...override })
180+
},
189181
[identity] // eslint-disable-line react-hooks/exhaustive-deps
190182
),
191183
})

0 commit comments

Comments
 (0)