import { __assign } from "tslib"; import { useCallback, useRef, useState } from 'react'; import useMountedState from './useMountedState'; export default function useAsyncFn(fn, deps, initialState) { if (deps === void 0) { deps = []; } if (initialState === void 0) { initialState = { loading: false }; } var lastCallId = useRef(0); var isMounted = useMountedState(); var _a = useState(initialState), state = _a[0], set = _a[1]; var callback = useCallback(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var callId = ++lastCallId.current; if (!state.loading) { set(function (prevState) { return (__assign(__assign({}, prevState), { loading: true })); }); } return fn.apply(void 0, args).then(function (value) { isMounted() && callId === lastCallId.current && set({ value: value, loading: false }); return value; }, function (error) { isMounted() && callId === lastCallId.current && set({ error: error, loading: false }); return error; }); }, deps); return [state, callback]; }