source: imaps-frontend/node_modules/react-use/esm/useAsyncFn.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import { __assign } from "tslib";
2import { useCallback, useRef, useState } from 'react';
3import useMountedState from './useMountedState';
4export default function useAsyncFn(fn, deps, initialState) {
5 if (deps === void 0) { deps = []; }
6 if (initialState === void 0) { initialState = { loading: false }; }
7 var lastCallId = useRef(0);
8 var isMounted = useMountedState();
9 var _a = useState(initialState), state = _a[0], set = _a[1];
10 var callback = useCallback(function () {
11 var args = [];
12 for (var _i = 0; _i < arguments.length; _i++) {
13 args[_i] = arguments[_i];
14 }
15 var callId = ++lastCallId.current;
16 if (!state.loading) {
17 set(function (prevState) { return (__assign(__assign({}, prevState), { loading: true })); });
18 }
19 return fn.apply(void 0, args).then(function (value) {
20 isMounted() && callId === lastCallId.current && set({ value: value, loading: false });
21 return value;
22 }, function (error) {
23 isMounted() && callId === lastCallId.current && set({ error: error, loading: false });
24 return error;
25 });
26 }, deps);
27 return [state, callback];
28}
Note: See TracBrowser for help on using the repository browser.