Explanation: in my hook I receive this get exercises from a page that I have, it executes well in the function "callEndpoint ", but at the moment of executing the await axiosCall.call; it does not return me a promise, it returns me a normal function, therefore I cannot solve the promise.
How can I do so that the hook returns me a promise? what is the error?
const useFetchAndLoad = () => {
const [loading, setLoading] = useState(false);
let controller = new AbortController();
const callEndpoint = async (axiosCall) => {
if (axiosCall.controller) controller = axiosCall.controller;
setLoading(true);
let result = {};
try {
result = await axiosCall.call;
} catch (err) {
setLoading(false);
throw err;
}
setLoading(false);
return result;
};
const cancelEndpoint = () => {
setLoading(false);
if (controller) {
controller.abort();
}
};
useEffect(() => {
return () => {
cancelEndpoint();
};
}, []);
return { loading, callEndpoint };
};
And I send this:
const getExercises = () => {
const controller = loadAbortAxios();
return {
call: axios.request(options.GET_EXCERSICES, { signal: controller.signal }),
controller: new AbortController(),
};
};