Skip to content

Commit cae8622

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

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

packages/react-async/src/useAsync.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,22 @@ const useAsyncFetch = (input, init, { defer, json, ...options } = {}) => {
166166
const accept = headers["Accept"] || headers["accept"] || (headers.get && headers.get("accept"))
167167
const doFetch = (input, init) => globalScope.fetch(input, init).then(parseResponse(accept, json))
168168
const isDefer =
169-
typeof defer === "boolean" ? defer : ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
169+
typeof defer === "boolean" ? defer : ["POST", "PUT", "PATCH", "DELETE"].indexOf(method) !== -1
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] = arg3 ? [arg1[0], arg3.signal] : [undefined, arg2.signal]
177+
if (typeof override === "object" && "preventDefault" in override) {
178+
// Don't spread Events or SyntheticEvents
179+
return doFetch(input, { signal, ...init })
180+
}
181+
return typeof override === "function"
182+
? doFetch(input, { signal, ...override(init) })
183+
: doFetch(input, { signal, ...init, ...override })
184+
},
189185
[identity] // eslint-disable-line react-hooks/exhaustive-deps
190186
),
191187
})

0 commit comments

Comments
 (0)