I watch tutorial about React and didnt understand how AbortController works.
useEffect(() => {
const abortCont = new AbortController();
setTimeout(() =>
{fetch(url, { signal: abortCont.signal })
.then(res => {
if(!res.ok) {
throw Error('could not fetch the data from that resource');
}
return res.json();
})
.then(data => {
setData(data);
setIsPending(false);
setError(null);
})
.catch((err) => {
if (err.name == 'AbortError'){
console.log('fetch aborted');
}
else {
setIsPending(false);
setError(err.message);
}
})}, 1000);
return () => abortCont.abort();
}, [url]);
why he uses return and why this code works when abort didnt happen if I call abort() anyway